Single instance

i want to know how to restrict second instance whiel first instance is running.any body plz help me in this regard.my id is deepikaachalla@gmail.com or deepika@nannacomputers.com
[199 byte] By [Deepika_nca] at [2007-10-3 5:39:56]
# 1

Do you mean you want to allow only one instance of your applicatoin to run at a time?

If so, then when it starts up, the first step for it to do is to try to open a ServerSocket on some port. If you can open it, that app is not already running (it doesn't already have the socket open). If you can't open it, then the app (or some app) is already running and already has that port.

When the app exits, the socket will automatically be released.

jverda at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 2
can you please let us know that for which you need one instance.. for servlet a java class or what ?
dipak66a at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 3

Hello,

i maybe have the same problem. I'm developing a Rich Client (eclipse based) with a derby embedded database. The Database allows only one session. To prevent the error of the application, i want that only one instance of the application exists.

i looked around in different forums and only found that following hint:

on start of the application checking the existence of a startup file on the filesystem. If the file isnt there, write the file and start the application. Else finish the application.

I'm not sure, if i'm in the right forum here. Sorry if not. Else i would be interested in the experiences other have.

with respect

maxe

maxea at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 4

> on start of the application checking the existence of

> a startup file on the filesystem. If the file isnt

> there, write the file and start the application. Else

> finish the application.

This is not as good as the socket mechanism described in reply 1. If your app crashes, it won't be remove the file, and the next time it tries to start, it will think one is already running when it's not.

jverda at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 5

> Hello,

>

> i maybe have the same problem. I'm developing a Rich

> Client (eclipse based) with a derby embedded

> database. The Database allows only one session. To

> prevent the error of the application, i want that

> only one instance of the application exists.

What do you mean "prevent the error"? Is an exception thrown when more than one client tries to connect? If so, then just handle it--that means there's more than one app trying to run.

jverda at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 6
Yes, i thought the file writing is a dirty way to handel this problem. I'm sorry, but i have no idea how the socket mechanism will run. Is it possible to get some example code for this socket mechanism?I'm very happy about your fast answer. Thank you jverd.
maxea at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 7
Yes, i handle the error and finish the application. That's a better way then writing a file. But you make me interesting in the socket mechanism. ;-)Thanks for your help and quick response. maxe
maxea at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 8
is it right, that the socket mechanism maybe have problems with firewalls?
maxea at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 9

> is it right, that the socket mechanism maybe have

> problems with firewalls?

No. You're not trying to open a connection to another host. You're just using a socket as a semaphore--a resource for which the OS will only allow one acquisition at a time.

It might have permission problems if the user running the Java app is not allowed to listen on the socket in question, or if there's a SecurityManager in place that's preventing it.

The code looks like this:

import java.net.ServerSocket;

import java.io.IOException;

import java.net.BindException;

public static void main(String[] args) {

try {

ServerSocket serverSocket = new ServerSocket(somePortThatYouPick);

}

catch (BindException exc) {

// Another process is listening on that port.

// Probably anoher instance of our app.

}

catch (IOException exc) {

// could not listen on that port for some other reason

}

// If we get here, no other app is listening on the port, so

// so no other instance of this app is running.

// When the app ends, the socket will be released

}

jverda at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 10
jverd, thanks for your information. That looks very easy. Ill try it out. Thank you for all the fast and good information. maxe
maxea at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 11

Hey, thanks for this example, it helped me make my app only run a single instance.

One small correction (and I may be wrong being the n00b programmer that I am), but the comment that says "If we get here, no other app.." will be reached regardless of exceptions thrown. It seems like you'd want to put your conditional code after the ServerSocket declaration.

Anyway, thanks again.

.andisblue.a at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 12
Also, which socket should I choose? I wouldn't want to pick a number that is common to another app. Can it be any number?
.andisblue.a at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 13
Another thing... I THOUGHT this was working.. but it only prevents another instance of the program from opening for the first 10 or so seconds, then it allows it... weird. Any explanation?
.andisblue.a at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 14

> Hey, thanks for this example, it helped me make my

> app only run a single instance.

>

> One small correction (and I may be wrong being the

> n00b programmer that I am), but the comment that says

> "If we get here, no other app.." will be reached

> regardless of exceptions thrown.

Oops. My bad. I'm so used to wrapping and rethrowing exceptions that I was imagining that inside the catch block.

If we rethrow from within catch, or just get rid of try catch altogether and propagate the exception (which I would not advise here), then my "if we get here, no other instance running" comment is accurate. But you're right--as it stands, we'll get there regardless.

> It seems like you'd

> want to put your conditional code after the

> ServerSocket declaration.

Yup. That's probably the best way. Just returning from the method at that point should suffice.

jverda at 2007-7-14 23:47:41 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 15

> Also, which socket should I choose? I wouldn't want

> to pick a number that is common to another app. Can

> it be any number?

Up through 1024 (I think) are reserved for well-known services. In general just pick a high number in the tens of thousands. You might want to do something like netstat or cport or something just to see what's used at the moment and increase your odds of guessing at one that's not used elsewhere.

jverda at 2007-7-21 11:11:27 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 16

> Another thing... I THOUGHT this was working.. but it

> only prevents another instance of the program from

> opening for the first 10 or so seconds, then it

> allows it... weird.

>

> Any explanation?

It sounds like something is auto-closing that ServerSocket. Maybe you have to do accept()? I've never needed to actually use this myself, and every time I've created a ServerSocket I've immediately done accept. I assumed it would work as I described, but I might have missed some detail.

Did you do your code just as I did above? (With the exception of where we get to if we get the socket, of course.)

Is the socket-holder in some thread that's dying?

- Edit -

Or maybe you're just not holding onto any reference to it, and the SS is gettng GCed and has a finalizer that closes it.

Message was edited by:

jverd

jverda at 2007-7-21 11:11:27 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 17

> Also, which socket should I choose? I wouldn't want

> to pick a number that is common to another app. Can

> it be any number?

Maybe something like that could be a solution, but I don't know if all that is worth of the result.

1. read the port number p from the configuration files

2. try to create a serversocket at port p

2.1. if 2. succeeds, all is ok. But you must communicate further with clients. goto 3.

2.2. if 2. fails, connect to p as a client, and communicate with it to ensure that the server is an instance of your application

2.2.1. if 2.2. succeeds, close the application. goto 3.

2.2.2. if 2.2. fails, that port is used by another sort of application. Choose random p, write it to the configuration file and goto step 1.

3. end

myavuzselima at 2007-7-21 11:11:27 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 18

Hi

We are also looking for the Stop running the java application twice solution.

We are considering : ServerSocket solution

But Is there any way to Find all the java applications running on PC and find whether it is same application or not? Is it possible?

Or Are there any other ways ( If the cusomer has problems with ports, firewall or any other problems with ServerSocket solution)

thanks lot.

kvally74a at 2007-7-21 11:11:27 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 19

Yeah, like getting instance of MyClass from all local JVM?!!

Since JDK 1.5 and above having something like "jps" to show all process, but they bind to some port too.

Is this a Java limitation? I would prefer someone from Sun Java to answer this.

Regards,

Avatar Ng

Message was edited by:

Avatar_Ng

Avatar_Nga at 2007-7-21 11:11:27 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 20
If you are running through WebStart, you can use a SingleInstanceService.SingleInstanceService sis = (SingleInstanceService)ServiceManager.lookup("javax.jnlp.SingleInstanceService");sis.addSingleInstanceListener(this);
MattCMCa at 2007-7-21 11:11:27 > top of Java-index,Other Topics,Java Community Process (JCP) Program...