Home arrow static arrow Java Programming [Archive] - I want to restrict instantiation of a class to one time only.
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - I want to restrict instantiation of a class to one time only.
This topic has 7 replies on 1 page.

Posts:2
Registered: 6/17/04
I want to restrict instantiation of a class to one time only.  
Jun 17, 2004 5:36 AM



 

I am developing a java application. The requirement is that only one instance of a class/application should run. Any attempt to load application twice be restricted displaying some warning message.

Please help me on this problem.
 

Posts:10,972
Registered: 10/23/03
Re: I want to restrict instantiation of a class to one time only.  
Jun 17, 2004 5:39 AM (reply 1 of 7)



 
It's a pity we can't apply the technology to mutiple postings.
 

Posts:24,036
Registered: 2/3/03
Re: I want to restrict instantiation of a class to one time only.  
Jun 17, 2004 5:40 AM (reply 2 of 7)



 
I am developing a java application. The requirement is
that only one instance of a class/application should
run. Any attempt to load application twice be
restricted displaying some warning message.

Please help me on this problem.

If you just want one instance of a class throughout an application, google for Singleton pattern.

I have no idea how (or why) you'd restrict only one instance of an application to be running at a time, unless the app always checks some service that keeps track of how many instances are running...
 

Posts:24,036
Registered: 2/3/03
Re: I want to restrict instantiation of a class to one time only.  
Jun 17, 2004 5:40 AM (reply 3 of 7)



 
It's a pity we can't apply the technology to mutiple
postings.

LOL. Everybody gets a little frisky with the submit button... :o)
 

Posts:7,499
Registered: 02-11-14
Re: I want to restrict instantiation of a class to one time only.  
Jun 17, 2004 5:44 AM (reply 4 of 7)



 

I am developing a java application. The requirement is
that only one instance of a class/application should
run. Any attempt to load application twice be
restricted displaying some warning message.

Please help me on this problem.

So, you want only one instance of the Application to run, not just one instance of a class IN an application?

There are several ways to do this, I believe. One is to create a specific temporary file in the file system. as soon as the application starts. First thing to do when the app runs (in the main method) would be to check if that file exists. If it does, exit the app. If not, create it then continue with the rest of the application.

there is a second method by opening up a port or socket. I am not familiar with this, but if you search the forum you should find an answer...
 

Posts:10,972
Registered: 10/23/03
Re: I want to restrict instantiation of a class to one time only.  
Jun 17, 2004 5:49 AM (reply 5 of 7)



 
There are several ways to do this, I believe. One is
to create a specific temporary file in the file
system. as soon as the application starts. First
thing to do when the app runs (in the main method)
would be to check if that file exists. If it does,
exit the app. If not, create it then continue with
the rest of the application.

there is a second method by opening up a port or
socket. I am not familiar with this, but if you
search the forum you should find an answer...

I would use ports. When an process stops it's guaranteed to release port resources. "temporary" files
can be left behind, to confuse your app the next time you run.

And just to be clear: OP, you wanted at most one copy of the app running on a given machine,
or one copying running anywhere on a network of machines?
 

Posts:9
Registered: 4/20/00
Re: I want to restrict instantiation of a class to one time only.  
Jun 17, 2004 3:44 PM (reply 6 of 7)



 
There is always the possibilty the 2nd application could start before the 1st opens the socket.
 

Posts:6,750
Registered: 1/25/04
Re: I want to restrict instantiation of a class to one time only.  
Jun 17, 2004 3:47 PM (reply 7 of 7)



 
But only one can get the socket, so whichever one opens the socket first wins, and the other one closes (or throws a hissy fit, or whatever the programmer decides).
 
This topic has 7 replies on 1 page.