| | |
Only 1 instance of program
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
When I open a Java Program, I want a method which returns true if the program is already running, and false otherwise. Is there a way to check for this?
For example, say we open our web browser, the first time we open it, i want the method to return false (there are no other instances of this program running). Now if I were to open a new copy of the program it would return true since there is already one copy of it open.
For example, say we open our web browser, the first time we open it, i want the method to return false (there are no other instances of this program running). Now if I were to open a new copy of the program it would return true since there is already one copy of it open.
There's no generic way.
One method that's generally advocated is to open a serversocket on an obscure port on the primary network interface on the first instance that starts.
This socket does nothing, it just sits there as a marker.
New instances that want to start cannot open a serversocket on that same port because only one server can run on any port on any given network interface.
One method that's generally advocated is to open a serversocket on an obscure port on the primary network interface on the first instance that starts.
This socket does nothing, it just sits there as a marker.
New instances that want to start cannot open a serversocket on that same port because only one server can run on any port on any given network interface.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
But what if a user downloads aol instant messenger, or some other internet program that uses the port the same as mine? BitTorrent (which uses serveral, yahoo messenger, there are a ton of programs which use a ton of ports). I obviously can't go and check to see which one of these all of these program use (especially since you can easily change there defaults)
lol, what if the program crahes or the operating system crashes/turns off/restarts/etc... and the file is left there for all eterninity! then the program will never be able to be opened again!
that's exactly why you should not use a lockfile UNLESS you also include a mechanism to remove the lockfile forcibly (which should then also cause any other instances of the application to terminate for example).
If another program tries to use that port it's out of luck, pure and simple.
The programs you mention are I think smart enough to hop to another port if the one they want isn't available (and in a mission commercial environment shouldn't be installed anyway).
If another program tries to use that port it's out of luck, pure and simple.
The programs you mention are I think smart enough to hop to another port if the one they want isn't available (and in a mission commercial environment shouldn't be installed anyway).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- Multi-user program issues (VB.NET)
- Confused writing a Java Class (Java)
- Ideas for a new XT compatible OS... (IT Professionals' Lounge)
Other Threads in the Java Forum
- Previous Thread: Controlling Hardware
- Next Thread: calling a method from another class
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows






