Phrixus
Phrixus

Reputation: 1219

Ant Script for running Java file with class path

I would like to simplify my life of running a Java file using a script. I think Ant script may work.

Each time I want to run my program (in Unix System), I have to write the following command

java -classpath ".:someclass" MyFile

Or if the user is using Windows:

java -classpath ".;someclass" MyFile

It is possible to create a script that will run the above commands (depending on the operating system, that is to understand what operating system is and to run the appropriate version of the command)?

Cheers

Upvotes: 0

Views: 136

Answers (1)

Rafael
Rafael

Reputation: 7242

The question is three month old, but I think is worth sharing the code as may will be helpful for others.

In general the idea of running a class from a .jar file is useful and has many applications.

The most straightforward solution to your problem is the following code:

   <!-- Classpath of Your Jar file -->
   <path id="dist.classpath"> 
      <pathelement location="dist/MyFile.jar"/> 
   </path> 

   <target name="someclass"> 
      <java fork="true" classname="YourJarClassName"> 
          <classpath refid="dist.classpath"/> 
      </java> 
   </target>  

You can now run:

ant someclass

And your YourJarClassName within the jar file will run. So note that the target name someclass is the name you will use to run the YourJarClassName class.

Upvotes: 1

Related Questions