Forum Stats

  • 3,735,134 Users
  • 2,247,117 Discussions


How to use singleinstance service with a JWS application

843802 Member Posts: 39,816
edited Jan 27, 2008 1:46AM in Java Web Start & JNLP

I have a Swing application and i am using Java web start to deploy on the user machines.
I want to know how to implement the single instance service. so that only one instance os the application is running.

Where should i use the singleinstance class provided by JNLP api.

for ex:

package test;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import javax.swing.border.EmptyBorder;
import java.util.Date;

// classes of the web-start API, used in this example.
import javax.jnlp.SingleInstanceListener;
import javax.jnlp.SingleInstanceService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;

/** A test of the SingleInstanceService using the web-start API.
@author Andrew Thompson
@version 2007/1/8
public class SingleInstanceApplication
extends JFrame
implements SingleInstanceListener {

/** A simple editing area. */
JTextArea document;

/** Assemble the GUI. */
SingleInstanceApplication() {
super("JNLP API single instance service");

try {
SingleInstanceService singleInstanceService =
// add the listener to this application!
(SingleInstanceListener)this );
} catch(UnavailableServiceException use) {


document = new JTextArea(
"Try openning another version of this application\n");

JPanel main = new JPanel(new BorderLayout());
main.add(new JScrollPane(document));
main.setBorder( new EmptyBorder(8,8,8,8) );



/** Specified by the SingleInstanceListener interface
@param args The command line parameters used for this invocation */
public void newActivation(String[] args) {
StringBuffer sb = new StringBuffer();
for (int ii=0; ii<args.length; ii++) {
sb.append("'" + args[ii] + "' ");
String message = "Got new args: " + sb.toString();
// this usually serves to alert the user the app.
// wants attention. On Win. it will flash the
// apps. icon in the task bar.
JOptionPane.showMessageDialog(this, message);
// also add the new args and time to the document.
document.append( new Date() + "\t" + message + "\n" );

/** Construct the GUI and display it. If the user double clicked
a file to start the application, begin measures to load that file. */
public static void main(String[] args) {
SingleInstanceApplication app =
new SingleInstanceApplication();

in my progrma there are many clasess where should i use this.

Pls help new bie!!

Thanks and Regards


  • 793415
    793415 Member Posts: 7,279
    edited Dec 14, 2007 8:51PM
    To work for your project, the SIS has to be implemented on the class that is the main(). To implement the SIS for a class, it needs to declare it implements the SingleInstanceListener and define the new activation method. Also, it needs to have the SingleInstanceService added to it via. the methods addSingleInstanceListener(). It is not strictly necessary to call addSingleInstanceListener() from the main() class, but it does make a lot of sense.

    Does that answer your question?

    Edited by: AndrewThompson64 on Dec 15, 2007 1:28 AM

    Edited by: AndrewThompson64 on Dec 15, 2007 1:48 AM
  • 843802
    843802 Member Posts: 39,816
    Thank you very much.
    Its solved my problem.

    Assigned the Duke stars
  • 843802
    843802 Member Posts: 39,816
    This example looks great. Unfortunately it seems to not work with JDK 1.6.0_03. Integrating this code with my app still allows me to launch multiple instances. Then I looked at Andrew's page which has the jnlps and jars ready to go as examples.... and same problem. I can run multiple instances of this example app from Andrew's page. Bummer. I really wanted this to work.
  • 843802
    843802 Member Posts: 39,816
    That's probably due to a known bug that was introduced in 1.6.0_03. Bug ID 6631056.
This discussion has been closed.