Sometime I see many application such as msn, windows media player etc that are single instance applications (when user executes while application is running a new applicatio
public class SingleInstance { public static final String LOCK = System.getProperty("user.home") + File.separator + "test.lock"; public static final String PIPE = System.getProperty("user.home") + File.separator + "test.pipe"; private static JFrame frame = null; public static void main(String[] args) { try { FileChannel lockChannel = new RandomAccessFile(LOCK, "rw").getChannel(); FileLock flk = null; try { flk = lockChannel.tryLock(); } catch(Throwable t) { t.printStackTrace(); } if (flk == null || !flk.isValid()) { System.out.println("alread running, leaving a message to pipe and quitting..."); FileChannel pipeChannel = null; try { pipeChannel = new RandomAccessFile(PIPE, "rw").getChannel(); MappedByteBuffer bb = pipeChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1); bb.put(0, (byte)1); bb.force(); } catch (Throwable t) { t.printStackTrace(); } finally { if (pipeChannel != null) { try { pipeChannel.close(); } catch (Throwable t) { t.printStackTrace(); } } } System.exit(0); } //We do not release the lock and close the channel here, // which will be done after the application crashes or closes normally. SwingUtilities.invokeLater( new Runnable() { public void run() { createAndShowGUI(); } } ); FileChannel pipeChannel = null; try { pipeChannel = new RandomAccessFile(PIPE, "rw").getChannel(); MappedByteBuffer bb = pipeChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1); while (true) { byte b = bb.get(0); if (b > 0) { bb.put(0, (byte)0); bb.force(); SwingUtilities.invokeLater( new Runnable() { public void run() { frame.setExtendedState(JFrame.NORMAL); frame.setAlwaysOnTop(true); frame.toFront(); frame.setAlwaysOnTop(false); } } ); } Thread.sleep(1000); } } catch (Throwable t) { t.printStackTrace(); } finally { if (pipeChannel != null) { try { pipeChannel.close(); } catch (Throwable t) { t.printStackTrace(); } } } } catch(Throwable t) { t.printStackTrace(); } } public static void createAndShowGUI() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 650); frame.getContentPane().add(new JLabel("MAIN WINDOW", SwingConstants.CENTER), BorderLayout.CENTER); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
If the app. has a GUI, launch it with JWS and use the SingleInstanceService
.
The Java Plug-In (required for both applets and JWS apps) was deprecated by Oracle and removed from the JDK. Browser manufacturers had already removed it from their browsers.
So this answer is defunct. Only leaving it here to warn people looking at old documentation.
If I believe this article, by :
having the first instance attempt to open a listening socket on the localhost interface. If it's able to open the socket, it is assumed that this is the first instance of the application to be launched. If not, the assumption is that an instance of this application is already running. The new instance must notify the existing instance that a launch was attempted, then exit. The existing instance takes over after receiving the notification and fires an event to the listener that handles the action.
Note: Ahe mentions in the comment that using InetAddress.getLocalHost()
can be tricky:
- it does not work as expected in DHCP-environment because address returned depends on whether the computer has network access.
Solution was to open connection withInetAddress.getByAddress(new byte[] {127, 0, 0, 1})
;
Probably related to bug 4435662.
getLocalHost
: return IP address of machine, vs. Actual results : return 127.0.0.1
. it is surprising to have
getLocalHost
return127.0.0.1
on Linux but not on windows.
Or you may use ManagementFactory object. As explained here:
The
getMonitoredVMs(int processPid)
method receives as parameter the current application PID, and catch the application name that is called from command line, for example, the application was started fromc:\java\app\test.jar
path, then the value variable is "c:\\java\\app\\test.jar
". This way, we will catch just application name on the line 17 of the code below.
After that, we search JVM for another process with the same name, if we found it and the application PID is different, it means that is the second application instance.
JNLP offers also a SingleInstanceListener
The Unique4j library can be used for running a single instance of a Java application and pass messages. You can see it at https://github.com/prat-man/unique4j. It supports Java 1.6+.
It uses a combination of file locks and dynamic port locks to detect and communicate between instances with the primary goal of allowing only one instance to run.
Following is a simple example of the same:
import tk.pratanumandal.unique4j.Unique4j;
import tk.pratanumandal.unique4j.exception.Unique4jException;
public class Unique4jDemo {
// unique application ID
public static String APP_ID = "tk.pratanumandal.unique4j-mlsdvo-20191511-#j.6";
public static void main(String[] args) throws Unique4jException, InterruptedException {
// create unique instance
Unique4j unique = new Unique4j(APP_ID) {
@Override
public void receiveMessage(String message) {
// display received message from subsequent instance
System.out.println(message);
}
@Override
public String sendMessage() {
// send message to first instance
return "Hello World!";
}
};
// try to obtain lock
boolean lockFlag = unique.acquireLock();
// sleep the main thread for 30 seconds to simulate long running tasks
Thread.sleep(30000);
// try to free the lock before exiting program
boolean lockFreeFlag = unique.freeLock();
}
}
Disclaimer: I created and maintain Unique4j library.
I wrote a dedicated library for that https://sanyarnd.github.io/applocker
It is based on file-channel locking, so it will not block a port number, or deadlock application in case of power outage (channel is released once process is terminated).
Library is lightweight itself and has a fluent API.
It was inspired by http://www.sauronsoftware.it/projects/junique/, but it's based on file channels instead. And there are other extra new features.
A more generic way of limiting the number of instance's on a single machine, or even an entire network, is to use a multicast socket.
Using a multicast socket, enables you to broadcast a message to any amount of instances of your application, some of which can be on physically remote machines across a corporate network.
In this way you can enable many types of configurations, to control things like
Java's multicast support is via java.net package with MulticastSocket & DatagramSocket being the main tools.
Note: MulticastSocket's do not guarantee delivery of data packets, so you should use a tool built on top of multicast sockets like JGroups. JGroups does guarantee delivery of all data. It is one single jar file, with a very simple API.
JGroups has been around a while, and has some impressive usages in industry, for example it underpins JBoss's clustering mechanism do broadcast data to all instance of a cluster.
To use JGroups, to limit the number of instances of an app (on a machine or a network, lets say: to the number of licences a customer has bought) is conceptually very simple :