This documentation has been the base of the official Commons-launcher project example.
The Jakarta Commons Launcher project provides a smart way to start a Java application.
In the facts, the Commons Launcher provides a bootstrap class which starts a ant process.
This ant process :
We want to start a small Main class :
package org.kolaka.jdf.demo; import org.apache.commons.logging.LogFactory; public class Main { public static void main(String[] args) { LogFactory.getLog(Main.class).info("start"); } }
We will name it “our application”
.
We planify this runtime distribution :
dist/demo/bin/jdf2-demo.sh : the user script to start our small Main class dist/demo/bin/ : contains the other files used by the scriptdist/demo/etc/ : contains the configuration file (log4j configuration, ..)dist/demo/lib/jdf2-demo.jar : contains the small Main classdist/demo/lib/ : contains the needed jar files to the applicationdist/demo/lib/launcher/ : contains the needed jar files to the commons launcher
the jdf2-demo.sh is the bootstrap script used by the user to start the application :
#!/bin/sh basedir=`dirname $0` java -cp $basedir LauncherBootstrap -executablename `basename $0` demo
our launcher.xml is as simple as possible :
<project name="JDF2 Demo Launcher" default="demo" basedir="."> <property name="base.dir" value="${basedir}/.."/> <property name="etc.dir" value="${base.dir}/etc"/> <property name="lib.dir" value="${base.dir}/lib"/> <property name="log.dir" value="${java.io.tmpdir}"/> <path id="base.class.path"> <pathelement path="${etc.dir}"/> <fileset dir="${lib.dir}" includes="*.jar"/> </path> <target name="demo"> <launch classname="org.kolaka.jdf.demo.Main"> <classpath refid="base.class.path"/> <syspropertyset> <sysproperty key="log.dir" file="${log.dir}"/> </syspropertyset> </launch> </target> </project>
It creates the needed classpath and executes our Main class.
LauncherBootstrap.class, commons-launcher.jar : the needed classes the Commons Launcherlauncher.properties : the bootstrap configuration (used to find the lib/launcher files)
This is the target which prepares the dist/bin and dist/demo/lib/launcher directories (the entire ant script is included into the example sources) :
<target name="dist.demo.bin"> <!-- create the lib/launcher directory with the lib files provided by the commons launcher --> <copy todir="dist/demo/lib/launcher"> <fileset dir="import/commons/launcher/lib"> <include name="*.jar"/> </fileset> </copy> <!-- copy the needed files into the bin directory --> <copy todir="dist/demo/bin" flatten="true"> <!-- from the commons launcher --> <fileset dir="import/commons/launcher/bin"> <include name="LauncherBootstrap.class"/> <include name="commons-launcher.jar"/> </fileset> <!-- from the application sources --> <fileset dir="source/org/kolaka/jdf/demo"> <include name="jdf2-demo.sh"/> <include name="launcher.xml"/> </fileset> </copy> <!-- make executable the shell scripts --> <chmod perm="+x"> <fileset dir="dist/demo/bin"> <include name="*.sh"/> </fileset> </chmod> <!-- create a property with the path to all 'lib/launcher' files --> <pathconvert property="dist.demo.bin.ant.class.path" pathSep=":" > <path> <fileset dir="dist/demo/lib/launcher"> <include name="*.jar"/> </fileset> </path> <map from="${basedir}/dist/demo" to=".."/> </pathconvert> <!-- creates the 'launcher.properties' with this path --> <propertyfile file="dist/demo/bin/launcher.properties"> <entry key="ant.class.path" value="${dist.demo.bin.ant.class.path}"/> </propertyfile> </target>