Check for a single instance of a java program

I have a Java program (with swing gui), and I would like for only one instance to ever exist. If he tried to open another instance of the program, I would like the current instance to be brought to the fore.

How to do it?

Thanks in advance.

+7
source share
6 answers

Launch the application using Java Web Start and run the SingleInstanceService JNLP API . Here is the diagram . SingleInstanceService .

If he tried to open another instance of the program, I would like the current instance to be brought to the fore.

Grab this in the newActivation (String []) SingleInstanceListener . Any arguments that were provided for the new launch will be passed to him. An existing instance may decide what to do with the new arguments (for example, modify the file, add a new tab, ignore ..)

+4
source

You can do this with ShutDownHook and a lock file, see this simple example .

I think this is the easiest way ...

+3
source

There is no previous instance in Java, but you can create a pid file in the temp directory (or / var / run). (And make it File.deleteOnExit() to clear it anyway on exit)

To move an existing window up, you can notify the program yourself, through a named pipe, unix socket, or calling remote Java methods, etc. A simple and dirty way is to write to a small file, for example $TEMP/foobar-app.bring-to-top , and the program should periodically test this small file, if this happens, return the window up and delete this small file.

I think Java cannot process signals, i.e. kill -HUP PID may not work for Java applications. Even if this is possible, not every OS has signals.

+2
source

I did this once with Socket and ServerSocket:


First, when starting the application, do ServerSocket listening on some port, for example 4004 . The trick is to check if it throws an IOException . If this is the case, either another application is running, or the port is being used by another application ( check this list for commonly used ports ; note that TCP and UDP ports do not block each other), otherwise you can continue to run the application. If the instance is currently running, you can notify it by plugging in a TCP Socket (which ensures that your connection is reached and UDP is not).

Here is an example:

 ServerSocket ss = null; try { ss = new ServerSocket(4004); } catch (IOException ex0) { // Port either occupied by your application or a foreign one // -> Connect Socket s = null; try { s = new Socket(); } catch (Exception ex1) { // Something went wrong } if (s != null) { // Send some singnal } } if (ss == null) { // Close or do something else } 

(I wrote this from my memory, so some things might be wrong or could be done better).

+1
source

In C #, you usually create Mutex when running Applicaiton. If you cannot create / get it, another instance of the application is already running. Unfortunately, I'm not 100% sure if this behaves the same in Java or the exact syntax.

Hope this helps.

0
source

Single template:

 class SingleInstance { private static SingleInstance instance; public SingleInstance getInstance() { if (instance==null) instance = new SingleInstance(); return instance; } private SingleInstance() { //construct it! } } 
-3
source

All Articles