Example - using web-start SingleInstanceService
After seeing a number of vague questions
relating to the web-start SingleInstanceService,
I decided to give it a try myself.
It seems to work 'as advertised' with very
little effort on the part of the developer.
You can get my simple example here.
The JNLP launch file and source (in a zip)
can be obtained from ..
http://www.physci.org/jws/
..look for the 'singleapp' files.
Here is the complete example..
src/java/test/SingleInstanceApplication.javapackage 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
*/
publicclass SingleInstanceApplication
extends JFrame
implements SingleInstanceListener{
/** A simple editing area. */
JTextArea document;
/** Assemble the GUI. */
SingleInstanceApplication(){
super("JNLP API single instance service");
try{
SingleInstanceService singleInstanceService =
(SingleInstanceService)ServiceManager.
lookup("javax.jnlp.SingleInstanceService");
// add the listener to this application!
singleInstanceService.addSingleInstanceListener(
(SingleInstanceListener)this );
}catch(UnavailableServiceException use){
use.printStackTrace();
System.exit(-1);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
document =new JTextArea(
"Try openning another version of this application\n");
document.setEditable(false);
JPanel main =new JPanel(new BorderLayout());
main.add(new JScrollPane(document));
main.setBorder(new EmptyBorder(8,8,8,8) );
getContentPane().add(main);
pack();
setSize(400,300);
setLocationRelativeTo(null);
}
/** Specified by the SingleInstanceListener interface
@param args The command line parameters used for this invocation */
publicvoid 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. */
publicstaticvoid main(String[] args){
SingleInstanceApplication app =
new SingleInstanceApplication();
app.setVisible(true);
}
}
src/conf/singleapp.jnlp><?xml version='1.0' encoding='UTF-8' ?>
<jnlp spec='1.0'
codebase='http://www.physci.org/jws'
href='singleapp.jnlp'>
<information>
<title>Single Instance Application</title>
<vendor>Andrew Thompson</vendor>
<description kind='one-line'>
Test of the JNLP SingleInstanceService
</description>
<shortcut online='false'>
<desktop/>
</shortcut>
</information>
<resources>
<j2se version='1.2+' />
<jar href='singleapp.jar' main='true' />
</resources>
<application-desc main-class='test.SingleInstanceApplication' />
</jnlp>
build.xml<!-- Build filefor the project. -->
<project basedir="." default="launch" name="singleapp">
<target name="properties">
<property name="build" value="build" />
<property name="dist" value="dist" />
<property name="src" value="src" />
<!-- Pot luck guess at location of
suitable'web-start' jar. -->
<property
name="classpath"
value="${java.home}/lib/javaws.jar" />
</target>
<target
name="compile"
depends="properties"
description="Compile the project" >
<mkdir dir="${build}/share" />
<javac
debug="on"
destdir="${build}/share"
srcdir="${src}/java"
source="1.2"
classpath="${classpath}" />
<copy todir="${build}/share">
<fileset dir="${src}/java">
<exclude name="**/CVS" />
<exclude name="**/*.java" />
</fileset>
</copy>
</target>
<target
name="dist"
depends="compile"
description="Create project distribution" >
<mkdir dir="${build}/jar" />
<mkdir dir="${build}/jar/lib" />
<jar destfile="${build}/jar/singleapp.jar">
<fileset dir="${build}/share">
<include name="**/*.class" />
</fileset>
</jar>
</target>
<target
name="make-launch-file"
depends="properties"
description="Copies and configures the launch file" >
<copy todir="${build}/jar" >
<fileset dir="${src}/conf" >
<include name="**/*.jnlp" />
</fileset>
</copy>
</target>
<target
name="launch"
depends="dist, make-launch-file"
description="Launch the project using webstart">
<exec executable="javaws"
dir="${build}/jar">
<arg line="-codebase file:. file:./singleapp.jnlp" />
</exec>
</target>
<target
name="uninstall"
depends="properties"
description="Uninstall the project from the webstart cache">
<exec executable="javaws">
<arg
line="-uninstall http://www.physci.org/jws/singleapp.jnlp"
/>
</exec>
</target>
<target name="clean"
depends="properties"
description="Clean all generated files">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
</project>

