8

I have a program in Java (with a swing gui), and I would like only 1 instance ever to exist. If it attempted to open another instance of the program I would like the current instance to be brought to the foreground.

How do I do this?

Thanks in advance.

5

6 Answers 6

4

Launch the application using Java Web Start and implement the SingleInstanceService of the JNLP API. Here is a demo. of the SingleInstanceService.

If it attempted to open another instance of the program I would like the current instance to be brought to the foreground.

Hook that up in the newActivation(String[]) method of the SingleInstanceListener. It will be passed any arguments that were provided for the new launch. The existing instance gets to decide what to do with the new args (e.g. change file, add new tab, ignore..)

Sign up to request clarification or add additional context in comments.

Comments

3

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

I think that it is the simplest way ...

1 Comment

I agree. This is probably the simplest approach to implement. This is what I've been using. There's a little trick on startup to get round the file delete bug on Windows.
2

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

To bring the existing window to top, you may notify the program yourself, thru named pipe, unix socket, or java remote method call, etc. A simple & dirty way is to write to a small file, say $TEMP/foobar-app.bring-to-top, and the program should periodically poll this small file, if it comes to exist, bring the window to top and remove this small file.

I guess Java couldn't handle signals, i.e., kill -HUP PID may not work for Java applications. Even if it could, not every OS have signals.

Comments

1

I did this once with a Socket and a ServerSocket:


First, when you start your application, make a ServerSocket listen on some port, for example 4004. The trick is to check whether it throws an IOException. If it does, there either is another application running or the port is used by another application (check this list for commonly used ports; Note that TCP and UDP ports are not blocking each other), otherwise you can continue with your application startup. If an instance is currently running, you might want to notify it by connecting a TCP Socket (which guarantees that your connection arrives; UDP doesn't).

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 out of my memory, so some things might be wrong or could be done better).

Comments

0

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

Hope this helps.

3 Comments

(Un)fortunately, Java mutexes are not shared between processes.
@alf Thanks for the info - learning something new every day... This makes my answer useless, but maybe there is another C# developer who didn't know too.
Not necessarily useless. The idea is there. You just have to find another way to implement the mutex.
-3

Pattern singletone:

class SingleInstance {
private static SingleInstance instance;

public SingleInstance getInstance() {
    if (instance==null)
        instance = new SingleInstance();
    return instance;
}

private SingleInstance() {
//construct it!
}
}

2 Comments

Using a singleton within an app doesn't prevent multiple instances of the app (i.e., multiple JVM instances).
That only helps within one classloader; OP wants something more like "only one instance on the computer"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.