I have a java application which start an another java application (third party) in background, so before launching third party background application I want to check whether that application is already running or not(don't want to wait for termination of that application).
I am using the following code for launching the third party java application :
String path = new java.io.File("do123-child.cmd").getCanonicalPath();
Runtime.getRuntime().exec(path);
Note : file "do123-child.cmd" call a ".bat" file to run that application.
To check whether a given application is running or not I am 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 to know whether is there any another way to do this without iterating the all processes currently running ? Like :
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 is running.
but above code return 0 for all application.
Thanks in advance.