Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, Checking if any process ID is currently running on Windows

Tags:

java

windows

pid

Is it possible to check the existence of process from Java in Windows.

I have its possible PID, I want to know if it is still running or not.

like image 342
Roman Avatar asked Mar 28 '10 18:03

Roman


1 Answers

How to check if a pid is running on Windows with Java:

Windows tasklist command:

The DOS command tasklist shows some output on what processes are running:

C:\Documents and Settings\eric>tasklist

Image Name                   PID Session Name     Session#    Mem Usage
========================= ====== ================ ======== ============
System Idle Process            0 Console                 0         28 K
System                         4 Console                 0        244 K
smss.exe                     856 Console                 0        436 K
csrss.exe                    908 Console                 0      6,556 K
winlogon.exe                 932 Console                 0      4,092 K
....
cmd.exe                     3012 Console                 0      2,860 K
tasklist.exe                5888 Console                 0      5,008 K

C:\Documents and Settings\eric>

The second column is the PID

You can use tasklist to get info on a specific PID:

tasklist /FI "PID eq 1300"

prints:

Image Name                   PID Session Name     Session#    Mem Usage
========================= ====== ================ ======== ============
mysqld.exe                  1300 Console                 0     17,456 K

C:\Documents and Settings\eric>

A response means the PID is running.

If you query a PID that does not exist, you get this:

C:\Documents and Settings\eric>tasklist /FI "PID eq 1301"
INFO: No tasks running with the specified criteria.
C:\Documents and Settings\eric>

A Java function could do the above automatically

This function will only work on Windows systems that have tasklist available.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class IsPidRunningTest {

    public static void main(String[] args) {

        //this function prints all running processes
        showAllProcessesRunningOnWindows();

        //this prints whether or not processID 1300 is running
        System.out.println("is PID 1300 running? " + 
            isProcessIdRunningOnWindows(1300));

    }

    /**
     * Queries {@code tasklist} if the process ID {@code pid} is running.
     * @param pid the PID to check
     * @return {@code true} if the PID is running, {@code false} otherwise
     */
    public static boolean isProcessIdRunningOnWindows(int pid){
        try {
            Runtime runtime = Runtime.getRuntime();
            String cmds[] = {"cmd", "/c", "tasklist /FI \"PID eq " + pid + "\""};
            Process proc = runtime.exec(cmds);

            InputStream inputstream = proc.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            String line;
            while ((line = bufferedreader.readLine()) != null) {
                //Search the PID matched lines single line for the sequence: " 1300 "
                //if you find it, then the PID is still running.
                if (line.contains(" " + pid + " ")){
                    return true;
                }
            }

            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Cannot query the tasklist for some reason.");
            System.exit(0);
        }

        return false;

    }

    /**
     * Prints the output of {@code tasklist} including PIDs.
     */
    public static void showAllProcessesRunningOnWindows(){
        try {
            Runtime runtime = Runtime.getRuntime();
            String cmds[] = {"cmd", "/c", "tasklist"};
            Process proc = runtime.exec(cmds);
            InputStream inputstream = proc.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            String line;
            while ((line = bufferedreader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Cannot query the tasklist for some reason.");
        }
    }
}

The Java code above prints a list of all running processes then prints:

is PID 1300 running? true
like image 197
Eric Leschinski Avatar answered Oct 17 '22 06:10

Eric Leschinski