How To List All The Processes Running On A Machine From Java Code?

A month again, I wished to search out out all processes operating on my machine from java code for some silly objective. What d you do in such a state of affairs? I attempted to jot down some code and was fairly profitable. Java cannot play with system course of and therefore invoking a runtime is simply resolution to get all course of and right here it’s:


import java.io.*;
class ListProcess

public static void major(String[] args) throws IOException

Runtime runtime = Runtime.getRuntime();

String cmds[] = "cmd", "/c", "tasklist";

Course of proc = runtime.exec(cmds);

InputStream inputstream = proc.getInputStream();

InputStreamReader inputstreamreader = new InputStreamReader(inputstream);

BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

String line;

whereas ((line = bufferedreader.readLine()) != null)

System.out.println(line);



Thoughts you, the code is written completely for Home windows Machine :). And one line change on this code will checklist you solely java operating course of.


String cmds[] = "cmd", "/c", "jps"; that is nothing however operating jps.exe file in bin (jdk6 onwards).

Its not all carried out. Writing Runtime code isn’t the true resolution as there’s little of platform dependencies. So, I’ve determined to jot down the code for getting Checklist of Java Course of. Once more, I’ve checked by OpenJDK code for jps(search on jps.java file 🙂 ) and I obtained some trace how you can do it and right here it goes:


import java.util.*;
import solar.jvmstat.monitor.*;
public class ListJavaProcess

public static void major(String[] args) throws Exception

/* Checking for native Host, one can do for distant machine as nicely */

MonitoredHost native = MonitoredHost.getMonitoredHost("localhost");

/* Take all lively VM's on Host, LocalHost right here */

Set vmlist = new HashSet(native.activeVms());

for (Object id : vmlist)

/* 1234 - Specifies the Java Digital Machine recognized by lvmid 1234 on an unnamed host.

This string is reworked into absolutely the type //1234, which have to be resolved in opposition to

a HostIdentifier. */

MonitoredVm vm = native.getMonitoredVm(new VmIdentifier("//" + id));

/* deal with class file and jar file each */

String processname = MonitoredVmUtil.mainClass(vm, true);

System.out.println(id + " ------> " + processname);



I’ve written good quantity of remark as it’s all collectively a solar import moderately than java or javax import. This import resides in instruments.jar, so even operating easy javac and java won’t work. So, operating this system will go right here:

E:Program FilesJavajdk1.6.0_10bin>javac -classpath “E:Program FilesJavajdk1.6.0_10libtools.jar” ListJavaProcess.java

E:Program FilesJavajdk1.6.0_10bin>java -classpath .;”E:Program FilesJavajdk1.6.0_10libtools.jar” ListJavaProcess 3700 ——> ListJavaProcess

Proper now just one java course of is operating. Now within the second code, you possibly can play with a number of the java course of, however with native course of within the above code you possibly can’t do something besides watching it 🙂

No concept how to do that in JDK 1.5 or backwards(runtime is astray one choice). Would like to study it another time.

Trending Merchandise

0
Add to compare
Umbro Youth (8-12) Size 4 Neo Futsal Pro Soccer Ball, White/Regal Blue/Vermillion
0
Add to compare
36.00
0
Add to compare
American Challenge todo Terreno al Aire Libre de Goma del balón de fútbol (Negro, 5)
0
Add to compare
25.00
0
Add to compare
Senda Athletics Rio Training Balón de Fútbol Sala, Unisex-Adult, Verde, Talla 4
0
Add to compare
29.17
0
Add to compare
adidas Tango Glider Soccer Ball, White/Black, Size 4
0
Add to compare
16.00
0
Add to compare
adidas Unisex-Adult Starlancer Club Soccer Ball, Team Royal Blue/White/White, 5
0
Add to compare
10.50
0
Add to compare
adidas Unisex-Adult Tiro League Sala White 3
0
Add to compare
40.00
0
Add to compare
American Challenge Brasilia balón de fútbol (limón, 5)
0
Add to compare
19.25
0
Add to compare
Uber Soccer Regulación tamaño y peso fútbol sala interior pelota de fútbol (azul marino brillante, 3)
0
Add to compare
24.95
0
Add to compare
Mitre Ultimatch – Balón de fútbol Sala, Unisex, Color Blanco/Rojo/Negro, Talla 4
0
Add to compare
31.70
0
Add to compare
VIZARI Balón de fútbol Unisex para jóvenes, Color Amarillo/Verde/Plateado, Talla 2
0
Add to compare
25.99
.

We will be happy to hear your thoughts

Leave a reply

ProSportGoods
Logo
Register New Account
Compare items
  • Total (0)
Compare
0
Shopping cart