Add an ant target to run a class from within a jar file
I have a jar file开发者_JS百科 with multiple executable classes, how can I run the main method of a using an ant target ?
Thanks
Take a look at the Ant Java Task. You should be able to create a target that looks like this:
<target name="mytarget" description="runs my class" >
<java classname="test.Main">
<classpath>
<pathelement location="dist/test.jar"/>
</classpath>
</java>
</target>
Alternative, using Ant Exec Task:
<target name="mytarget" description="runs my class">
<exec executable="java">
<arg line="-classpath dist/test.jar test.Main"/>
</exec>
</target>
Using ant's java task:
<java fork="yes" classname="com.example.Class" failonerror="true">
<classpath>
<pathelement path="path/to/jar/containing/the/com.example.Class"/>
...
</classpath>
...
</java>
First you have to decide which class is used as entry point.
Let's assume that the class is com.mycompany.Main
in this case if you wish to run application from command line say
java -cp my.jar com.mycompany.Main
Now you can either run it as java program:
<java classname="com.mycompany.Main">
<classpath>
<pathelement location="myjar.jar"/>
</classpath>
</java>
(see http://ant.apache.org/manual/Tasks/java.html)
or run it as an generic external process:
(see http://ant.apache.org/manual/Tasks/exec.html).
I think that using java target is preferable.
精彩评论