arun raj
arun raj

Reputation: 11

Unable to kill a process when executing jar file using taskkill

I have an exe triggered from within a java swing application. I am able to kill the exe using "taskkill /PID ProcessID " when running the application from ECLIPSE IDE . But when i try to run the jar file for the swing application through a batch file, the exe doesn't get terminated probably because I am unable to obtain the process ID. I am using windows XP 32 bit. Any help will be greatly appreciated

String sDosCommand = "cmd /c tasklist /FI " + "\"" + "IMAGENAME eq " + sProcessName + "\"" ;
Process process = Runtime.getRuntime().exec(sDosCommand ); 

This code (modified to get process ID of one particular process) gives me the Process ID, which in turn I use in Taskkill command executed similarly

Thanks and regards Arun Raj

Upvotes: 1

Views: 1240

Answers (2)

arun raj
arun raj

Reputation: 11

I figured out where the problem was.I was triggering the application jar from a batch file . This batch was also modifying the environment varibles(PATH to be precise) which was preventing me from executing the TASKKILL command . I have made a slight modification to the batch file to include the PATH environment variable as well.This solves my problem.

Thanks to all for their help. Arun Raj

Upvotes: 0

Guillaume Polet
Guillaume Polet

Reputation: 47608

I am assuming that you are using ProcessBuilder to start your external application. If you are not, consider using that class and its start() method to launch the external application, as explained in the Javadoc.

Once you have called start(), you will get a Process, on which you can call destroy() to kill the external application.

Process p = new ProcessBuilder("myCommand", "myArg").start();
...
p.destroy(); // this kills the command "myCommand"

Upvotes: 1

Related Questions