Run a jar file with ant
Ant is a very useful tool. We can use it to compile, package jar files, and run the packaged jar. Following is example to describe how to write the build.xml file to run a packaged jar file with ant.
The jar file's name is ddtool.jar, and the main class of it is com.sun.ddtool.manager.DDToolManager. If the jar file is executed manually in CLI mode, the running command will be as follows:
$ java -classpath ./lib/commons-codec-1.3.jar:./lib/commons-httpclient-3.0.1.jar:./lib/commons-logging-1.1.jar:./ddtool.jar -Djava.library.path=lib com.sun.ddtool.manager.DDToolManager http://129.158.218.41 DriverDB/ dblocation.xml.zip
NOTE: "http://129.158.218.41", "DriverDB/", and "dblocation.xml.zip" are 3 arguments passed to the main class.
In the build.xml, we can describe it like this:
<!--class path-->
<path id="execute-classpath">
<fileset dir="${dist.bin.dir}/">
<include name="ddtool.jar"/>
</fileset>
<fileset dir="${lib.dir}/">
<include name="*.jar"/>
</fileset>
</path>
<!--run-->
<target name="execute" depends="packjar" description="run the project">
<java classname="com.sun.ddtool.manager.DDToolManager" failonerror="true" fork="true">
<classpath refid="execute-classpath"/>
<sysproperty key="java.library.path" value="lib/"/>
<arg value="http://129.158.218.41"/>
<arg value="DriverDB/"/>
<arg value="dblocation.xml.zip"/>
</java>
</target>
Or, if you don't want to define the execute-classpath, the above targets could be replaced by the following one:
<target name="execute" depends="packjar" description="run the project">
<exec executable="java">
<arg line="-classpath ${dist.lib.dir}/commons-httpclient-3.0.1.jar:${dist.lib.dir}/commons-codec-1.3.jar:${dist.lib.dir}/commons-logging-1.1.jar:${dist.bin.dir}/ddtool_21.jar -Djava.library.path=${dist.lib.dir} com.sun.ddtool.manager.DDToolManager http://129.158.218.41 DriverDB/ dblocation.xml.zip"/>
</exec>
</target>
NOTE: "${dist.lib.dir}/commons-httpclient-3.0.1.jar:${dist.lib.dir}/commons-codec-1.3.jar:${dist.lib.dir}/commons-logging-1.1.jar" in the second example cannot be replaced with "${dist.lib.dir}/*.jar".
Posted at 03:25PM May 05, 2008 by Ye Julia Li in Java | Comments[0]