This is a simple Java Program which grabs all running Process on system and prints each process line by line.
We will use class Runtime
. Every Java application has a single instance of class Runtime
that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime
method.
An application cannot create its own instance of this class.
Note: we are using command ps -few
to grab all processes.
package com.crunchify.tutorials; import java.io.BufferedReader; import java.io.InputStreamReader; /** * @author Crunchify.com * */ public class CrunchifySystemProcess { public static void main(String[] args) { try { String process; // getRuntime: Returns the runtime object associated with the current Java application. // exec: Executes the specified string command in a separate process. Process p = Runtime.getRuntime().exec("ps -few"); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((process = input.readLine()) != null) { System.out.println(process); // <-- Print all Process here line // by line } input.close(); } catch (Exception err) { err.printStackTrace(); } } }
Windows command:
System.getenv("windir") +"\\system32\\"+"tasklist.exe /fo csv /nh"
Output:
UID PID PPID C STIME TTY TIME CMD 0 1 0 0 18Sep13 ?? 6:43.95 /sbin/launchd 0 11 1 0 18Sep13 ?? 0:07.98 /usr/libexec/UserEventAgent (System) 0 12 1 0 18Sep13 ?? 0:07.13 /usr/libexec/kextd 0 14 1 0 18Sep13 ?? 0:38.41 /usr/sbin/notifyd 0 15 1 0 18Sep13 ?? 1:10.88 /usr/sbin/securityd -i 0 16 1 0 18Sep13 ?? 0:01.90 /usr/sbin/diskarbitrationd 0 17 1 0 18Sep13 ?? 3:46.28 /usr/libexec/configd 0 18 1 0 18Sep13 ?? 2:45.31 /System/Library/CoreServices/powerd.bundle/powerd 0 19 1 0 18Sep13 ?? 0:18.40 /usr/sbin/syslogd 0 20 1 0 18Sep13 ?? 0:49.48 /usr/sbin/distnoted daemon 0 21 1 0 18Sep13 ?? 0:29.08 /usr/sbin/cfprefsd daemon 0 31 1 0 18Sep13 ?? 1:02.99 /System/Library/CoreServices/coreservicesd ..... ..... .....