Solved

Execute a unix 'ps' command in Java

Posted on 1997-12-21
2
598 Views
Last Modified: 2006-11-17
How can I execute a unix 'ps' command in unix with Java ?
0
Comment
Question by:templer122197
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
ashishkh earned 150 total points
ID: 1231780
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
ID: 1231781
Excellent
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 122
null output 3 42
Facing this issue for maven proxy setting 2 27
Netbeans and org.apache.commons.lang3 issue 3 26
In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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