views:

91

answers:

2

In Java or via some Eclipse plugin, is it possible to find a process by name without dropping down to native code? Specifically, I want to determine if a web browser is running and let the user know they may need to restart the browser.

I know native code is always an option but I want to avoid setting up another JNI library if I can avoid it.

A: 

I don't know any way to get a list of running processes directly from Java. If you're on Linux, you could create a Process that executes "ps", read the Process's InputStream to intercept the results, and parse it. You wouldn't need to go down to the JNI level to do that: You can execute a shell command with Process.

Jay
+1  A: 

It's actually a duplicate question, see the original one.

You have two options here, one is to run the command line tool that will list the running processes and parse its output. As mentioned by Jay on Linux one can use "ps", on Windows you would need to bundle some tool, either a port of ps from GnuWin/cygwin/etc or a native tool like PsList, or look at the sample.

Second option, which you already know about is to use JNI, however you don't need to write your code from scratch, you may consider JniWrapper. It provides an easy to use Java API for common native OS functions. They also provide free licenses for Open Source projects.

CrazyCoder
Thanks for the duplicate pointer; I tried a search but clearly went the wrong way with my search.I only need to care about Windows and OSX, so scraping the output should be sufficient for what I'm doing.
Scott K.