简体   繁体   中英

Add an ant target to run a class from within a jar file

I have a jar file 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM