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.
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
> 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.
> 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.
> 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
}
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.
> 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.
> 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.
> 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
> 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
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.
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