Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect via Java whether a particular process is running under Windows?

Tags:

java

Well the title pretty much sums the question. The only thing I found is this but I'm not sure if thats the way to go.

like image 867
gmunk Avatar asked Feb 23 '10 12:02

gmunk


People also ask

How do you check if a process is running in Windows using Java?

Find the full command line of your java application Say hello to Windows Task Manager. Go to 'Processes' tab. You will see the list of processes running on the system. Bam!

How do you check if a process is running using Java?

If you want to check the work of java application, run 'ps' command with '-ef' options, that will show you not only the command, time and PID of all the running processes, but also the full listing, which contains necessary information about the file that is being executed and program parameters.

How do you check if a process is running on Windows?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column. Click on any column name to sort.


1 Answers

You can use the wmic utility to check the list of running processes.
Suppose you want to check if the windows' explorer.exe process is running :

String line;
try {
    Process proc = Runtime.getRuntime().exec("wmic.exe");
    BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
    oStream .write("process where name='explorer.exe'");
    oStream .flush();
    oStream .close();
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }
    input.close();
} catch (IOException ioe) {
    ioe.printStackTrace();
}

See http://ss64.com/nt/wmic.html or http://support.microsoft.com/servicedesks/webcasts/wc072402/listofsampleusage.asp for some example of what you can get from wmic...

like image 127
Philippe Avatar answered Oct 19 '22 11:10

Philippe