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.

rustybeanstalk
  • 2,722
  • 9
  • 37
  • 57
  • 6
    http://stackoverflow.com/questions/2528844/only-one-swing-frame-window-opened-at-time – onurbaysan Nov 22 '11 at 07:50
  • @mKorbel there's no need to post any code, the question is clear and self-containing. The answer by Judas Imam is perfect. – alf Nov 22 '11 at 07:59
  • Alternative ways to implement single instance app: http://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application –  Nov 22 '11 at 08:16
  • 1
    http://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application or http://stackoverflow.com/questions/7397769/java-single-instance-software-with-socket-issue-in-closing-socket-under-windows – StanislavL Nov 22 '11 at 08:25
  • 1
    Does this answer your question? [How to implement a single instance Java application?](https://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application) – Alexander Biryukov Nov 07 '20 at 00:24

6 Answers6

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

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

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

I think that it is the simplest way ...

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 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. – James P. Apr 01 '12 at 15:21
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.

Lenik
  • 13,946
  • 17
  • 75
  • 103
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).

machinateur
  • 502
  • 5
  • 10
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.

Bernhard Kircher
  • 4,132
  • 3
  • 32
  • 38
  • 5
    (Un)fortunately, Java mutexes are not shared between processes. – alf Nov 22 '11 at 08:01
  • 1
    @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. – Bernhard Kircher Nov 22 '11 at 08:03
  • Not necessarily useless. The idea is there. You just have to find another way to implement the mutex. – James P. Apr 01 '12 at 15:22
-3

Pattern singletone:

class SingleInstance {
private static SingleInstance instance;

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

private SingleInstance() {
//construct it!
}
}
execc
  • 1,083
  • 12
  • 25