Solved

Execute a unix 'ps' command in Java

Posted on 1997-12-21
2
582 Views
Last Modified: 2006-11-17
How can I execute a unix 'ps' command in unix with Java ?
0
Comment
Question by:templer122197
2 Comments
 

Accepted Solution

by:
ashishkh earned 150 total points
Comment Utility
public class TestPs
{
        public static void main(String args[])
        {
          try
          {
                Runtime rt = Runtime.getRuntime();
                Process ps = rt.exec("ps");
                ps.waitFor();
                System.out.println("The output of process is " + ps.getOutputStr
eam());
                System.out.println("The output of process is " + ps.exitValue())
;
           }
           catch(Exception e)
           {
                System.err.println(e.getMessage());
           }
        }

}

The above java code executes unix 'ps' command and prints its exit value. I am printing the output of ps command. You can do so by getting the OutputStream of the Process using getOutputStream method of Process class. Then you can print the output stream of that process using normal read calls.
Hope this solves the problem.
0
 

Author Comment

by:templer122197
Comment Utility
Excellent
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now