Java - get the PID of an external process on the command line in Windows 7

I have Windows 7 32 bit with Java:

How to get the process PID on the command line in Windows 7?

I want to kill an application that I can really identify by the command line that launched it. There are several Java applications running on this computer. I need to stop certain.

To be precise: I need to find a tomcat that runs catalina.bat . What do you think is the best way to do this?

I know the task list, but it seems that it cannot request the command line that started the process. The search for java.exe does not help me. I tried to find something useful, such as pgrep / pkill for Windows, without success.

+4
source share
5 answers

Finally found something. The solution that works for me is called wmic (Command Management Commandmentation). This good tool is built into Windows 7 Pro (mine) and, possibly, to other versions of Windows. It provides a fairly large number of actions, such as listing all running tasks, taking into account all the details (for example, the corresponding command line), various information about the equipment, etc. Exactly what I need.

0
source

You can use the jps -lv to define a java process using command line options. jps is a utility that is included in many modern JDKs.

+8
source

Java, get the PID of the currently running process in Windows

This should work on Linux , OSX , Windows and JVM's HotSpot.

 import sun.management.VMManagement; import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; import java.lang.reflect.Field; import java.lang.reflect.Method; public static int getCurrentPID() { try{ java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean(); java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm"); jvm.setAccessible(true); sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime); java.lang.reflect.Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId"); pid_method.setAccessible(true); return (Integer) pid_method.invoke(mgmt); } catch(Exception e){ e.printStackTrace(); System.out.println("Failed at getting the process ID"); System.exit(0); } } 

Call it like this:

 System.out.println("PID: " + getCurrentPID()); 

For me, it prints the process id: PID: 5728

Sources:

How can a Java program get its own process id?

http://boxysystems.com/index.php/java-tip-find-process-id-of-running-java-process/

+3
source

Try it on the command line:

 sc queryex type= service state= all | find "APP" 

Where APP is the name of the program. This command will return all services matching this.

Then you can run SC QUERYEX APP and it will return the status and PID number.

Once you have the PID:

 TASKKILL /f /pid ### 

Where ### is the actual PID

+2
source

If you just need to kill a specific tomcat from a Java application, why not encode a simple servlet running inside each tomcat that will respond to a get request with a string that identifies it. Then use another servlet to execute something like:

 System.exit(-1); 
+1
source

All Articles