Recently I had a programming challenge in that I estimated a feature for this Java application that needed to know whether a certain program was running, and if it was running, terminate it.

Of course, in Java, it cannot be done in a cross-platform way. You can certainly launch a process in Java, then terminate that process you created, but you cannot know if a certain process is running and terminate that. To do this, you will need to have native code, and the way Java interfaces with your native code is through JNI (Java Native Interface).

In subsequent blog posts, I will write about how I coded this “Process Manager” JNI library using NetBeans – creating a process management library that can can return a list of running processes (and their process ids), as well as terminating the process by process id. The libraries I created were implemented in C, using NetBeans, on the Mac OS X, Windows, and Linux (Ubuntu) platforms using NetBeans 6.0.1.

It turns out it was easiest in Linux: you can easily list all the running processes through Java code. Processes are directories under “/proc”, with the directory filename as process id. Inside the directory itself is a text file with the filename of “status”, and the first line contains the process name. Of course to terminate this process by process id, you will still need to use native code.