Process List and Handles in Java

C

Chris Clark

I want to be able to get a list of processes running (Windows XP) and
see if my process (a Java App) is running on my system. If it is not
running I can just supply command line arguments to make my program
open up with a specific file, but in the case where I already have an
instance of my program open I want to continue to have just that one
instance. With the process id of my java program running, I then want
to be able to send it a message to open a file in the existing
instance of the program running, and not open a new instance of it. By
doing this i will just be able to send it my file and it will open in
my java program that is already open.

I know this is possible in C#, but I'm working with java and I'm not
sure if you can get a list of processes and send around a window's
handle so I can send it a message to open the specific file. This
feature is similar to many other apps that make it so files only open
in one window, so there aren't multiple instances floating around.

I hope I made sense.

Thanks,
Chris
 
G

Gordon Beaton

I want to be able to get a list of processes running (Windows XP) and
see if my process (a Java App) is running on my system. If it is not
running I can just supply command line arguments to make my program
open up with a specific file, but in the case where I already have an
instance of my program open I want to continue to have just that one
instance. With the process id of my java program running, I then want
to be able to send it a message to open a file in the existing
instance of the program running, and not open a new instance of it. By
doing this i will just be able to send it my file and it will open in
my java program that is already open.

Don't muck around with process lists.

Instead, have your application open a ServerSocket on a particular
port number. The OS will prevent other processes from opening a
ServerSocket that uses the same port.

If you start the application and are unable to open your ServerSocket
(i.e. if you get an "address already in use" exception) assume that
the application is already running. In that case, you can use a Socket
to connect to the running application and pass it whatever commands
you like.

Another way is to have the application write its pid to a file in a
well-known place. Subsequent instances of the program can look there
for the pid of the running program. Since the pid file will get left
behind if your application crashes, you need to do further checks
before deciding whether the application is already running.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top