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 ListProcesspublic 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 ListJavaProcesspublic 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