0

I'm trying to code a task manager for Linux using Java.

I need to get a list of running programs. And other info like: memory usage, cpu usage ...

Is this possible from Java?

Thanks.

3 Answers 3

6
try {
    // Execute command
    String command = "ps aux";
    Process child = Runtime.getRuntime().exec(command);

    // Get the input stream and read from it
    InputStream in = child.getInputStream();
    int c;
    while ((c = in.read()) != -1) {
        process((char)c);
    }
    in.close();
} catch (IOException e) {
}

Source (modified): http://www.exampledepot.com/egs/java.lang/ReadFromCommand.html

Sign up to request clarification or add additional context in comments.

2 Comments

Nice! Much more descriptive than my answer!
Thanks, but I more or less copypasted the example from the page mentioned ;)
1

It is possible, in systems that use the /proc virtual filesystem, you can just transverse the directories and cat out the information under /proc.

The numbered directories in /proc are the process ids of running processes, and the items within those directories help describe the process.

For memory usage and cpu information, there are /proc/meminfo and /proc/cpuinfo (and a lot more). Hopefully that will get you started.

For systems that lack the /proc virtual filesystem, you need to use JNI to bind to C code which will do kernel API calls, or attempt to run local command line programs thorough an exec while piping and parsing the output back into the Java program.

Comments

0

Try using the exec(String command). You can then get the input stream from the resulting Process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.