I am using jdk1.6 to develop an application which should list only particular processes running in a RHEL 5 server. I had used 'Runtime.getRuntime().exec("ps -ef|grep java")' to call linux process but does not work. It works if I use "ps -ef" as command.
Can anyone suggest me, how to run joined command of linux through application.
P.S. Since you are using JDK1.6 you can use ProcessBuilder rather than Runtime.exec(). Runtime.exec() uses ProcessBuilder behind the scenes but has an improved API compared to Runtime.exec(). The 'traps' article still apples to Process objects created using ProcessBuilder.
Ritesh Dwa wrote:It works if I use "ps -ef" as command.
OK, so use that. I presume you're familiar with String.matches().
However, on a general note, Java was never designed for running system commands; indeed, it runs contrary to the whole ethos of Java - portability.
Your solution, for example, will only work on a Unix/Linux system.
On the other hand, it should be a very simple matter to write a script that runs the pipeline you want and sends the output of that to a Java program.
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Joined: Sep 18, 2012
Thank you all for your suggestion.
I have implemented the suggestion and now the application can handle the command smoothly.