Hi all, My client application sends a message to my server after every 10 sec.Server sends the response.After getting the response ,the client application needs to start a process(run a jar file).So before starting ,it needs to check,wheather that process is already running or not.How should I do this in java?
If the additional process is one you have developed, then you can use a pure-Java way of finding out whether it is running. The easiest is to get it to open an agreed socket. Your other program can then look to see if that socket is open. A more robust approach might be to actually send some agreed message down the socket.
If you can't do this, then you need to inspect the running processes. That cannot be done in pure Java. You therefore need a solution per platform. The two basic approaches are either (a) run an external console process (e.g. "ps") to list the processes, capture its output and parse it in Java, (b) write some native code and interface it to your Java with JNI. The latter option is faster and cleaner, but harder to program.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
If your client progam is the only one that going to run that jar file, then you can have a static variable to indicate if that process has been ever started before.
If the new process that you start is not a child of your client program and when the client process restarts for some reason then you might end up in a situation where your static varible has wrong value.
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad: