• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to list process of linux in Java application?

 
Ritesh Dwa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
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.

Thank you.


Best Regards,
Ritesh
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"does not work" is not very informative.
Exec'ing can be tricky with the handling of the output streams.

| (pipe) and redirections (<,>, etc.) are handled by the way by the shell, so you might need something like this


 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and you need to read all the 4 sections of http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and implement ALL the recommendations.

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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Ritesh Dwa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your suggestion.

I have implemented the suggestion and now the application can handle the command smoothly.

Best Regards,
Ritesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic