Click to See Complete Forum and Search --> : detecting if there's already an instance of application in the JVM
zacharya
January 24th, 2005, 10:06 PM
I want to make my aplication only have one instance in one machine...
in order to do that I have to detect is there already an instance of the application in the JVM. In win32 API I just look up the hInstance.....
but in Java....how do I able to do that?
thanks before.....
Joe Nellis
January 25th, 2005, 03:11 AM
You can open DatagramSocket or maybe a ServerSocket on a particular port. If you get a java.net.BindException then there is already another application running. That is the easiest I can think of. If the application hangs or dies I am not sure what the status of the port binding would be, you'd have to read the docs or experiment a bit.
jsun
February 13th, 2005, 11:49 AM
Does anyone have sample code on this case?
dlorde
February 13th, 2005, 12:10 PM
Just try to open a ServerSocket on a specified port. If it fails, another instance is already running. The ServerSocket will be released if the holder dies.
Eighty percent of success is showing up...
W. Allen
pkpks
February 21st, 2005, 05:38 AM
hi,
using win xp SP2,
a pop up shows up asking to block/unblock the application trying to access the port.
can this be avoided?
regards,
Prashant
Joe Nellis
February 21st, 2005, 01:18 PM
No. Opening a port on a system with an active firewall program running will most likely produce an alert on any system. You can tell the user its benign if you want or try to determine singleton status of your program another way. Another way would be to write to a common file name.
fransn
February 25th, 2005, 09:58 AM
Or you could do it the same way Pointbase locks a database connection, by creating a file with a few characters on startup and deleting it on exit.
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.