| View previous topic :: View next topic |
| Author |
Message |
Scott Collier Guest
|
Posted: Sat Nov 29, 2003 3:24 am Post subject: Prevent the same program from running more than once. |
|
|
I need help...
I have designed a program that works fine until somebody opens the program
again (without closing the first program).
Is there a way to prevent a second instance of the same program running on
the one JVM.
So if somebody double clicks on the shortcut to the program, it will check
if the program is already running... is this possible ?
Any pointers or advice would be great.
Scott
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.545 / Virus Database: 339 - Release Date: 27/11/03
|
|
| Back to top |
|
 |
Real Gagnon Guest
|
|
| Back to top |
|
 |
Jazz Jezebel Guest
|
Posted: Sat Nov 29, 2003 11:24 am Post subject: Re: Prevent the same program from running more than once. |
|
|
My news feed looks blocked - apologies if I'm repeating someone.
You need to use the Singleton pattern - here's a simple example:
+++++++++++++++++++++++++++++++++++
public class Singleton {
private static final Singleton _theInstance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return _theInstance;
}
}
++++++++++++++++++++++
The constructor is private - to use the object is Singleton.getInstance()
See:
http://developer.java.sun.com/developer/qow/archive/111/index.html
http://developer.java.sun.com/developer/technicalArticles/Programming/singletons/
Julia
--
Jazz Jezebel
"Scott Collier" <noSpam (AT) thisAddress (DOT) com> wrote
| Quote: | I need help...
I have designed a program that works fine until somebody opens the program
again (without closing the first program).
Is there a way to prevent a second instance of the same program running on
the one JVM.
So if somebody double clicks on the shortcut to the program, it will check
if the program is already running... is this possible ?
Any pointers or advice would be great.
Scott
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.545 / Virus Database: 339 - Release Date: 27/11/03
|
|
|
| Back to top |
|
 |
Adam Guest
|
Posted: Mon Dec 01, 2003 8:34 am Post subject: Re: Prevent the same program from running more than once. |
|
|
"Jazz Jezebel" <jdbr20766 (AT) SPAM (DOT) TRAP.blueyonder.co.uk> wrote
| Quote: | My news feed looks blocked - apologies if I'm repeating someone.
You need to use the Singleton pattern - here's a simple example:
|
Won't work.
Diffrent JVMs.
regs,
Adam
|
|
| Back to top |
|
 |
Gregory A. Swarthout Guest
|
Posted: Mon Dec 01, 2003 10:04 pm Post subject: Re: Prevent the same program from running more than once. |
|
|
Real Gagnon <real_ (AT) _rgagnonSpamIsBadstripunderscore (DOT) com> wrote
I use a similar technique, except that when you start up instance two
of a program, it shuts down instance one.
|
|
| Back to top |
|
 |
Scott Collier Guest
|
Posted: Tue Dec 02, 2003 9:36 am Post subject: Re: Prevent the same program from running more than once. |
|
|
So far this seems like the way to go... have tried it out and it seems to
work...
I tried to use a different port number eg 19999 but this did not run as
smoothly,
it threw an exception about the port already being used or something...
(when second instance
was created).
"Gregory A. Swarthout" <gregorys (AT) xmission (DOT) com> wrote
| Quote: | Real Gagnon <real_ (AT) _rgagnonSpamIsBadstripunderscore (DOT) com> wrote
Any pointers or advice would be great.
see http://www.rgagnon.com/javadetails/java-0288.html
for a solution using socket.
bye.
I use a similar technique, except that when you start up instance two
of a program, it shuts down instance one.
|
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.545 / Virus Database: 339 - Release Date: 27/11/03
|
|
| Back to top |
|
 |
Jonas Kongslund Guest
|
Posted: Tue Dec 02, 2003 10:21 am Post subject: Re: Prevent the same program from running more than once. |
|
|
Scott Collier wrote:
| Quote: | So far this seems like the way to go... have tried it out and it seems to
work...
|
Another solution is to use a lock file.
| Quote: | I tried to use a different port number eg 19999 but this did not run as
smoothly,
it threw an exception about the port already being used or something...
(when second instance
was created).
|
Just catch the exception and exit gracefully.
--
Jonas Kongslund
|
|
| Back to top |
|
 |
Scott Collier Guest
|
Posted: Thu Dec 04, 2003 11:51 am Post subject: Re: Prevent the same program from running more than once. |
|
|
| Quote: | I tried to use a different port number eg 19999 but this did not run as
smoothly,
it threw an exception about the port already being used or something...
(when second instance
was created).
Just catch the exception and exit gracefully.
--
Jonas Kongslund
|
Thanks Jonas, however I was just intrigued as to why it worked with port 80
and not 19999. Why isn't there an exception with port 80 ?
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.545 / Virus Database: 339 - Release Date: 27/11/03
|
|
| Back to top |
|
 |
|