windows - To check whether an application is running or not using java? -


i have java application start java application (third party) in background, before launching third party background application want check whether application running or not(don't want wait termination of application).
using following code launching third party java application :

string path = new java.io.file("do123-child.cmd").getcanonicalpath(); runtime.getruntime().exec(path); 

note : file "do123-child.cmd" call ".bat" file run application.

to check whether given application running or not using following code [ ref link ]:

boolean result = false; try {     string line;     process p = runtime.getruntime().exec("tasklist.exe");     bufferedreader input =             new bufferedreader(new inputstreamreader(p.getinputstream()));     while ((line = input.readline()) != null) {         if(line.startswith("myapp.exe")){             result = true;             break;         }      }      input.close(); } catch (exception err) {      err.printstacktrace(); } return result; 

i want know whether there way without iterating processes running ? :

process p = runtime.getruntime().exec("tasklist /fi \"imagename eq myapp.exe\" /nh"); int exitval = p.exitvalue(); //if above code throw "java.lang.illegalthreadstateexception" means application running. 

but above code return 0 application.

in advance.

you use jps inspect java applications running. jps bundled jre.

jps -l 19109 sun.tools.jps.jps 15031 org.jboss.main 14040  14716 

you scrape list program using runtime.getruntime().exec() , reading input stream, search package names match within java.

since want avoid iterating results, grep result using findstr return basic p.exitvalue() result looking for:

process p = runtime.getruntime().exec("jps -l | findstr /r /c:\"com.myapp.myapp\""); int exitval = p.exitvalue(); // returns 0 if running, 1 if not 

of course findstr windows-specific, you'll need use grep instead on mac:

process p = runtime.getruntime().exec("jps -l | grep \"com.myapp.myapp\""); int exitval = p.exitvalue(); // returns 0 if running, 1 if not 

the jps tool uses internal api (monitoredhost) obtain information, entirely within java well:

string processname = "com.myapp.myapp";  boolean running = false; hostidentifier hostidentifier = new hostidentifier("local://localhost");  monitoredhost monitoredhost; monitoredhost = monitoredhost.getmonitoredhost(hostidentifier);  set activevms = monitoredhost.activevms(); (object activevmid : activevms) {     vmidentifier vmidentifier = new vmidentifier("//" + string.valueof(activevmid) + "?mode=r");         monitoredvm monitoredvm = monitoredhost.getmonitoredvm(vmidentifier);     if (monitoredvm != null) {         string mainclass = monitoredvmutil.mainclass(monitoredvm, true);         if (mainclass.tolowercase().equals(processname.tolowercase())) {             running = true;             break;         }     } }  system.out.print(running); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -