2

I want to get a list with all running processes using Java code?. Can you tell me from which file I can get this information? I would like to get his data from /proc filesystem.

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

3 Answers3

8

Just parse the input obtained by running Process p = Runtime.getRuntime().exec("ps -e");

I assume you use linux since you have tagged the question with linux.

By the way this one is related: How to get a list of current open windows/process with Java?

Community
  • 1
  • 1
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
3

For Linux,

Process process = Runtime.getRuntime().exec("ps -e");
BufferedReader processReader =  new BufferedReader(new InputStreamReader(process.getInputStream()));
// Read from BufferedReader

From windows

 Process process = Runtime.getRuntime().exec (System.getenv("windir") +"\\system32\\"+"tasklist.exe");
    BufferedReader processReader =  new BufferedReader(new InputStreamReader(process.getInputStream()));
    // Read from BufferedReader
Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46
2

The numerically named folders in /proc contain information on the processes on your system.

Getting the contents of those into a human readable format would require some effort in directory traversal, reading documentation and code related to the /proc FS, and is too wide an area to go into detail with here, but concrete issues related to this would be great SO followup questions :-)

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55