Forums Register Login

Using unix commands in java

+Pie Number of slices to send: Send
Hi All,

I am able to retrieve and store file in the unix box using java.(by importing com.jcraft.jsch package).

Now I need to pass unix commands (cat, grep) in java. How can i do that?

I read the forum and used the below code to search for success string in filename.log.



And I get the following error

Cannot run program "cat": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)


Can any one help me out?
+Pie Number of slices to send: Send
The working directory is most likely not what you expected it to be - you should use absolute paths.
+Pie Number of slices to send: Send
Also, "cat filename.log | grep success" is not one process. It's one process ("grep success") that is using the output of another process ("cat filename.log") as its input. This is handled by the shell (BASH, KSH, CSH etc) usually. When doing this in Java you need to do this yourself.

I see some options:
1) copy the contents of the input stream of the first process to the output stream of the second process, then read the input of the second process:


2) replace the catting with a Java input stream:


3) replace it all with Java code:
If the regex becomes a little more complex you can use java.util.regex.Pattern instead of the contains method.

Personally, I would go for option three. If you don't, read this first.
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3708 times.
Similar Threads
Help with runtime.exe and javac from command line
executing linux commands through java socket
Problem in process
How to connect to Linux using Java code?
Runtime.getRuntime( ).exec( command ); Win vs Unix
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:02:20.