• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to list process of linux in Java application?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic