How To Checklist All The Procedures Managing On A Equipment From Java Code?

0
11
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  


A month back, I preferred to discover out all processes jogging on my machine from java code for some stupid intent. What d you do in this sort of a circumstance? I attempted to write some code and was quite productive. Java can’t participate in with procedure method and therefore invoking a runtime is only option to get all method and in this article it is:

import java.io.*
class ListProcess 

community static void main(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

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

Technique.out.println(line)

Brain you, the code is created completely for Home windows Equipment :). And one particular line improve in this code will list you only java functioning course of action.

String cmds[] = "cmd", "/c", "jps" this is nothing at all but operating jps.exe file in bin (jdk6 onwards).

Its not all completed. Crafting Runtime code is not the serious remedy as there is minimal of system dependencies. So, I have made a decision to create the code for obtaining Checklist of Java Procedure. Yet again, I have checked by OpenJDK code for jps(research on jps.java file 🙂 ) and I obtained some trace how to do it and here it goes:

import java.util.*
import sunshine.jvmstat.keep track of.*
general public course ListJavaProcess 

community static void main(String[] args) throws Exception 

/* Checking for community Host, a person can do for distant machine as perfectly */

MonitoredHost regional = MonitoredHost.getMonitoredHost("localhost")

/* Consider all active VM's on Host, LocalHost right here */

Set vmlist = new HashSet(local.activeVms())

for (Item id : vmlist) 

/* 1234 - Specifies the Java Virtual Device recognized by lvmid 1234 on an unnamed host.

This string is transformed into the absolute sort //1234, which need to be solved from

a HostIdentifier. */

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

/* consider care of class file and jar file both of those */

String processname = MonitoredVmUtil.mainClass(vm, real)

Procedure.out.println(id + " ------> " + processname)

I have created excellent sum of remark as it is all collectively a sun import instead than java or javax import. This import resides in resources.jar, so even operating straightforward javac and java will not perform. So, managing the system will go below:

E:Plan FilesJavajdk1.6._10bin>javac -classpath “E:System FilesJavajdk1.6._10libtools.jar” ListJavaProcess.java

E:Software FilesJavajdk1.6._10bin>java -classpath .”E:Application FilesJavajdk1.6._10libtools.jar” ListJavaProcess 3700 ——> ListJavaProcess

Proper now only a person java course of action is managing. Now in the second code, you can enjoy with some of the java course of action, but with native process in the previously mentioned code you can not do nearly anything apart from observing it 🙂

No thought how to do this in JDK 1.5 or backwards(runtime is off training course just one choice). Would adore to learn it some other time.

Supply by Vaibhav Kumar Choudhary

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

LEAVE A REPLY

Please enter your comment!
Please enter your name here