• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ending java application when it detects another of its instance

 
cle tan
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Winston Gutkowski
Bartender
Pie
Posts: 9463
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
cle tan
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9463
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic