Share the post "How To Implement A Single Instance Java Web Start Or Applet"
I think it started with Java 1.6 that a JNLP jar file was included that can let you run a Java web start application or applet in a single instance.
Through the SingleInstanceService class, you application or applet will be able to detect if another instance is run when it is called from a JNLP.
I have not tried this with an applet that is run from an <APPLET> tag but I tested this in an applet if it is called from a JNLP.
Simply implement SingleInstanceListener from your class and initialize it with this code:
|
1 2 3 4 5 6 |
try { SingleInstanceService singleInstanceService = (SingleInstanceService) ServiceManager.lookup("javax.jnlp.SingleInstanceService"); singleInstanceService.addSingleInstanceListener((SingleInstanceListener) this); } catch(UnavailableServiceException use) { Logger.getLogger(JViewerAsFrame.class.getName()).log(Level.SEVERE, null, use); } |
And in the newActivation() method, you can put in a message prompt that looks something like this:
|
1 2 3 4 |
@Override public void newActivation(String[] strings) { JOptionPane.showMessageDialog(this, "Only one instance is allowed.", "Error", JOptionPane.ERROR_MESSAGE); } |
Easy, right?
By the way, if this code is run within an IDE, it will return an exception. This will have to be run from a JNLP in order for it to work.