Java - get PID of external process by command line in Windows 7

12,355

Solution 1

Finally found something. The solution working for me is called wmic (Windows Management Instrumentation Commandline). This nice tool comes built-in to Windows 7 Pro (mine) and possibly other Windows versions. It provides a quite large variety of actions like listing all the running tasks with every details you can imagine (like their corresponding command line), various hardware info, etc. Exactly what I need.

Solution 2

You could use jps -lv command to determine java process by it's command line options. jps is utility that included in many up-to-date JDKs.

Solution 3

Try in a command prompt:

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

Where APP is the name of the program. This command will return all services that match that.

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

Once you have the PID:

TASKKILL /f /pid ###

Where ### is the actual PID

Solution 4

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

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

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);
    }
}

Invoke it like this:

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

For me it prints the processID: 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/

Solution 5

If you just need to kill a specific tomcat from a java application why not coding a simple servlet running inside each tomcat that will responde to a get request with a string that will identify it. Then use another servlet to execute something like:

System.exit(-1);
Share:
12,355
annih
Author by

annih

Updated on June 05, 2022

Comments

  • annih
    annih almost 2 years

    I have Windows 7 32 bit with Java:

    How do I get the PID of a process by command line in Windows 7?

    I want to kill an application which I only can truly identify by the command line which ran it. We have several Java applications running on that machine. I need to stop specific ones.

    To be exact: I need to find tomcat which is run by catalina.bat. What do you think is the best way to do this?

    I know of tasklist, but it does not seem to be able to query the command line which started the process. Finding java.exe does not help me. I tried searching for something useful like pgrep/pkill for Windows, with no success.

  • annih
    annih almost 12 years
    Thanks. Nice idea. That also came to my mind just after I posted this question here, but does not solve my issues in the long run. It should work for now but in the future I will need to shutdown other Java applications as well.
  • annih
    annih almost 12 years
    Great idea. Didn't know jps. This will work for a while. Nevertheless I'm still interested if there's a system-wide solution. (I already accepted that there won't be any system-independent solution.)