AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Prevent the same program from running more than once.

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Scott Collier
Guest





PostPosted: Sat Nov 29, 2003 3:24 am    Post subject: Prevent the same program from running more than once. Reply with 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
Real Gagnon
Guest





PostPosted: Sat Nov 29, 2003 3:59 am    Post subject: Re: Prevent the same program from running more than once. Reply with quote



Quote:
Any pointers or advice would be great.

see http://www.rgagnon.com/javadetails/java-0288.html
for a solution using socket.

bye.
--
Real Gagnon from Quebec, Canada
* Looking for Java or PB snippets ? Visit Real's How-to
* http://www.rgagnon.com/howto.html

Back to top
Jazz Jezebel
Guest





PostPosted: Sat Nov 29, 2003 11:24 am    Post subject: Re: Prevent the same program from running more than once. Reply with quote



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





PostPosted: Mon Dec 01, 2003 8:34 am    Post subject: Re: Prevent the same program from running more than once. Reply with quote


"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





PostPosted: Mon Dec 01, 2003 10:04 pm    Post subject: Re: Prevent the same program from running more than once. Reply with quote

Real Gagnon <real_ (AT) _rgagnonSpamIsBadstripunderscore (DOT) com> wrote

Quote:
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.

Back to top
Scott Collier
Guest





PostPosted: Tue Dec 02, 2003 9:36 am    Post subject: Re: Prevent the same program from running more than once. Reply with quote

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





PostPosted: Tue Dec 02, 2003 10:21 am    Post subject: Re: Prevent the same program from running more than once. Reply with quote

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





PostPosted: Thu Dec 04, 2003 11:51 am    Post subject: Re: Prevent the same program from running more than once. Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.