Single Instance Java Application

A single-instance application starts and checks whether it is the first instance or a subsequent instance of the application:

  • If it is the first instance, it starts as usual.
  • If it is a subsequent instance, then the single-instance application is already running. The subsequent instance passes its parameters to the existing single-instance application.

For example consider this scenario:

Your application is running. You have some files that are associated with it. When you open one of those files the application will not be launched again, but the running instance will be used to open the file.

In order for your application to be capable of running in Single Instance Mode, in the main class of your java application add a method with one of the following signatures:

static void secondaryMain( String args[] )
static int secondaryMain( String args[] )

This method will be called if you choose to instantiate the application again.

ImportantThe second method should be used when you want your application to provide a return code when running in single instance mode. The application's return code is given by the return value of this method.