A month back, I preferred to locate out all processes functioning on my equipment from java code for some stupid intent. What do you do in this sort of a state of affairs? I attempted to compose some code and was very profitable. Java can not engage in with process approach and due to the fact invoking a runtime is only alternative to get all course of action and below it is:
import java.io. * class ListProcess public static void principal (String  args) throws IOException Runtime runtime = Runtime.getRuntime () String cmds  = "cmd", "/ c", "tasklist" Procedure proc = runtime.exec (cmds) InputStream inputstream = proc.getInputStream () InputStreamReader inputstreamreader = new InputStreamReader (inputstream) BufferedReader bufferedreader = new BufferedReader (inputstreamreader) String line whilst ((line = bufferedreader.readLine ())! = null) System.out.println (line)
Intellect you, the code is published solely for Windows Equipment :). And one line alter in this code will checklist you only java jogging system.
String cmds  = "cmd", "/ c", "jps" This is nothing but running jps.exe file in bin (jdk6 onwards).
Its not all done. Composing Runtime code is not the genuine alternative as there is minimal of platform dependencies. So, I have determined to create the code for finding Record of Java Procedure. Yet again, I have checked by OpenJDK code for jps (search on jps.java file :)) and I received some trace how to do it and below it goes:
import java.util. * import solar.jvmstat.observe. * general public course ListJavaProcess general public static void primary (String  args) throws Exception / * Examining for community Host, a single can do for distant machine as well * / MonitoredHost regional = MonitoredHost.getMonitoredHost ("localhost") / * Just take all active VM's on Host, LocalHost right here * / Established vmlist = new HashSet (area.activeVms ()) for (Item id: vmlist) / * 1234 - Specifies the Java Virtual Equipment discovered by lvmid 1234 on an unnamed host. This string is converted into the absolute kind // 1234, which will have to be resolved towards a HostIdentifier. * / MonitoredVm vm = area.getMonitoredVm (new VmIdentifier ("//" + id)) / * take treatment of class file and jar file both of those * / String processname = MonitoredVmUtil.mainClass (vm, accurate) System.out.println (id + "------>" + processname)
I have prepared great quantity of remark as it is all with each other a sun import alternatively than java or javax import. This import resides in tools.jar, so even operating easy javac and java will not work. So, functioning the application will go right here:
E: Application FilesJavajdk1.6._10bin> javac -classpath “E: Application FilesJavajdk1.6._10libtools.jar” ListJavaProcess.java
E: System FilesJavajdk1.6._10bin> java -classpath . “E: Application FilesJavajdk1.6._10libtools.jar” ListJavaProcess 3700 ——> ListJavaProcess
Right now only a person java course of action is managing. Now in the second code, you can engage in with some of the java procedure, but with native course of action in the earlier mentioned code you can not do anything apart from watching it 🙂
No plan how to do this in JDK 1.5 or returns (runtime is off training course one particular alternative). Would adore to study it some other time.