views:

202

answers:

5

I have an external Windows .exe that is actually Java application: Running the .exe starts javaw.exe, which in turn runs that Java application.

I didn't write that application and have no access to it through an API. I need to be able to kill it, however. So right now I just kill the Windows process javaw.exe, which is fine for a test machine running only that Java application but if I need finer granularity, I cannot currently do so.

My searches yielded suggestions such as Sysinternal's Process Explorer or the jps command in the JDK, but in the target systems for which I intend to provide the script, neither JDK nor Sysinternal's Process Explorer can be running.

Is there any other way that doesn't require an external tool? Does javaw.exe have a switch or command line option that lists Java processes? Is there a JRE version of jps?

Thanks.

+2  A: 

I'd still suggest just killing the javaw.exe.

I can't see the downside, since it is the process you want to kill after all.

Remember that if you run multiple applications on the machine, they should each have a separate JVM instance. So you can still kill the specific application if you need to.

mikera
Thank you both, Jim and @mikera -- +1 for both of you for explaining this issue to me. How would I know *which* javaw.exe to kill?
Android Eve
Can't you look in Task Manager's Applications tab, then right-click on the relevant program and hit Go To Process. That'll give you the right javaw.exe.
Robert Grant
What you need is a tool like Sysinternals Process Explorer. It'll allow you to see the parent processes and show you the full command line passed to javaw.exe.
Danny Thomas
+2  A: 

If you want to kill an entire JVM, just kill the javaw.exe process. Within a JVM there can be multiple Java threads but there's no way to poke into a JVM and terminate a thread unless the developer of the application provided a method to do so.

Jim Garrison
A: 

Based on your comment, multiple javaw.exe programs are running and you need to know which one to kill.

You might want to try connecting to each of the processes with JConsole and inspect the JVM. There may be enough clues to determine which one to kill. Once you've identified the profile of your application, you should be able to script the logic to make it easier in the future (use JMX to get most of the information provided by JConsole).

Jim Rush
A: 

The JDK (and possibly the JRE) ship with a utility called jps which can list all Java processes but also tell you the Main-Class currently running in that JVM. If JMX/JConsole is not an option, simply parsing the output of "jps -ml" and killing the appropriate process may work.

basszero
A: 

If the executable launches javaw then exits without providing any further information it seems like you need to use your scripting language to take a snapshot of running processes on the machine before launching the executable and after the executable has finished. Then you'll be able to deduce which is the new javaw process. What scripting language are you using?

Fredrick Pennachi