Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how to list all running processes in a system?

frizvifrizvi Posts: 14Member
I am making a project for a bank (MCB) in Java. The objective is to make a task manager in java that can run on JVM and in the end on Unix environment.

I am having problem in getting the list of all the processes running currently on the system. Can anyone help me with this?

I will be thankful,

Faraz H.

Comments

  • arb123arb123 Posts: 696Member
    Shell ps
    [code]
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;

    public class PsTest {

    public static String getUnparsedProcessList() {
    InputStream in = null;
    InputStream buffer = null;
    Reader reader = null;
    try {
    Process p = Runtime.getRuntime().exec("ps");
    in = p.getInputStream();
    buffer = new BufferedInputStream(in);
    reader = new InputStreamReader(buffer);
    char[] charr = new char[1024];
    StringBuffer strbuff = new StringBuffer();
    while(true) {
    int r = reader.read(charr);
    if(r<=0) {
    break;
    }
    strbuff.append(charr, 0, r);
    }
    return strbuff.toString();
    }catch(IOException e) {
    //todo - error handling
    e.printStackTrace();
    } finally {
    if(reader!=null) {
    try {
    reader.close();
    } catch(IOException e) {
    //failsafe
    }
    }
    if(buffer!=null) {
    try {
    buffer.close();
    } catch(IOException e) {
    //failsafe
    }
    }
    if(in!=null) {
    try {
    in.close();
    } catch(IOException e) {
    //failsafe
    }
    }
    }
    return null;
    }

    public static void main(String[] args) {
    System.out.println(getUnparsedProcessList());
    }

    }
    [/code]
    You can change the switches to find the format you want to parse.

    ---------------------------------
    [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • frizvifrizvi Posts: 14Member
    Thankx for the help man!
    But the code you gave is still producing the following exception.
    Note that it still displays "null" on the output

    java.io.IOException: CreateProcess: ps error=0

    at java.lang.Win32Process.create(Native Method)

    at java.lang.Win32Process.(Win32Process.java:63)

    at java.lang.Runtime.execInternal(Native Method)

    at java.lang.Runtime.exec(Runtime.java:566)

    at java.lang.Runtime.exec(Runtime.java:428)

    at java.lang.Runtime.exec(Runtime.java:364)

    at java.lang.Runtime.exec(Runtime.java:326)

    at serverclient.Server.getUnparsedProcessList(Server.java:93)

    at serverclient.Server.main(Server.java:48)

    null

  • arb123arb123 Posts: 696Member
    ps is a Unix command; it is not present on Win32 systems. Win32 does not provide a similar command-line command (at least not by default - Windows NT Utilities and Windows Services for Unix may provide what you want, but these won't be installed in most systems). You will probably have to write classes that use JNI to talk to native C++ code to do something like this on Win32.

    ps is a POSIX Unix command you can use to list running processes; other commands like kill can be used to manipulate them. You should be able to invoke the system commands on any POSIX-compliant system without needing to write + compile native code to the local OS/architecture. I do not know if the output of these commands is identical on all Unix systems (Linux, Solaris, AIX, and so on); testing will be required.

    ---------------------------------
    [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • frizvifrizvi Posts: 14Member
    Thankx Man!
    Im sorry for asking such a stupid question but I had a real hard working day yesterday. Now I have completed the project and thankx to you :).

    Just one last thing ... i you can give me your email address so that i can contact u in the future much more easily?

    Anyways Man! THANKX A MILLION

    Faraz H.
  • Nisa1207Nisa1207 Posts: 1Member
    Salam

    I am working on the same objective but I fail to get its solution. Can you provide that piece of code?

    Thanks I will be very thankful to you.
  • rizwananjumrizwananjum lahorePosts: 1Member

    any one help me to solve the problem related to oop i will show it when some ask about it other wise i do it self

Sign In or Register to comment.