As Ivan says: you can; but it's likely to be fraught.
Why do you want to do this? Far better to me would be to have the second instance issue an error message if there is one already running.
Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
Joined: Jun 11, 2012
Some applications like mine will cause problems when there are 2 or more instances of the same java application running
it has already cause some of my file proceses(such as deletion, moving of files and logging of files
I searched and find I also can use batch file to prevent the running of the same instance.
Reserve a port for the application. When the application start it should try to create a server socket on the reserved port. If it succeeds then it must be the only instance running so it can continue. If it fails to create the server socket then it opens a connection to the reserved port and tells the other application to terminate. It should then loop trying to create a server socket on the reserved port until it succeeds (with maybe a timeout if thought necessary).
Joined: Aug 05, 2005
cle tan wrote:
Joanne Neal wrote:
cle tan wrote:3. The first instance of java application terminates. The second continues running.
Is that what is happening or what you want to happen ?
i.e. is this a problem description or a requirements statement ?
this is what I want to happen
Why ? If your first instance has already started processing, wouldn't it be better to leave it running and just shut down the second instance immediately.
If this is an option, then it makes implementing what Richard Tookey suggested easier as you would just need to try to create a server socket on the reserved port and if it fails (because the first instance is running) just shut down the second instance.
Richard Tookey wrote:Reserve a port for the application. When the application start it should try to create a server socket on the reserved port.
Hmmm. Nifty that. Wish I'd thought of it.
Not meaning to usurp this thread, but is there any difference between Windows and Unix/Linux when it comes to port numbering? Also, do you not risk colliding with an unrelated process like PFTP that uses random or grouped ports for communication?
Joined: Aug 05, 2005
Winston Gutkowski wrote:Also, do you not risk colliding with an unrelated process like PFTP that uses random or grouped ports for communication?
That is a potential problem. An alternative is to use a lock file. The first instance creates a file and the second instance checks for the existence of this file. Obviously the 'live' instance has to remember to delete the file when it exits, which may be a problem if the program crashes. There are ways around this. These are just two of the commonly suggested methods for checking for other instances of your program. there are probably others - each with their own advantages/disadvantages.
This question periodically surfaces on the Java forums and boils down to the server socket solution. The lock files expose different behavior on the different platforms, and if the program crashes, a lock file remains, whereas the server socket is released by the OS if the process quits for any reason.