blue-sky
blue-sky

Reputation: 53916

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

Upvotes: 1

Views: 13445

Answers (3)

dogbane
dogbane

Reputation: 274828

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>

Upvotes: 7

AlexR
AlexR

Reputation: 115388

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.

Upvotes: 2

khachik
khachik

Reputation: 28703

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>

Upvotes: 3

Related Questions