wood burning stoves 2.0*
The moose likes Java in General and the fly likes Ending java application when it detects another of its instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Ending java application when it detects another of its instance" Watch "Ending java application when it detects another of its instance" New topic
Author

Ending java application when it detects another of its instance

cle tan
Ranch Hand

Joined: Jun 11, 2012
Posts: 68
Is there any way I can do this?

1.Run java application in 1 command window
2.Run java application again in another command window.
3. The first instance of java application terminates. The second continues running.

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Two instances of the same program are two separate processes. It takes extra effort to make them know about each other.
The termination of the first instance will not terminate the second one.

I do not see your problem.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
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 ?


Joanne
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

cle tan wrote:Is there any way I can do this?

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.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
cle tan
Ranch Hand

Joined: Jun 11, 2012
Posts: 68
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.

http://stackoverflow.com/questions/11619416/identify-running-instances-of-a-batch-file



But I would like someone to enlighten me as there is no full solution to this problem
cle tan
Ranch Hand

Joined: Jun 11, 2012
Posts: 68
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

but apparently when i run 2 instance of application are running
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

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).
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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?

Winston
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

The main difference between Windows and Unix has to do with rights. In Unix you need root rights to start listening on a port below 1024, whereas I believe Windows doesn't have this limitation.

As for the ports, you should always follow port lists like on http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml or
http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers, and look for unassigned numbers. You still may be running into a port conflict if another program uses the same port though, but at least your changes will be a bit better.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ending java application when it detects another of its instance