Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a command line argument to JAR in an Ant script

Tags:

java

ant

I've got a executable JAR file. And I've got an Ant build script that compiles and then creates this JAR file. I would like a task to run the JAR file as well, but I've got a command line argument that needs to be passed to the JAR. It's a configuration file. The run target is below

<target name="run">
    <java jar="build/jar/ShoutGen.jar" fork="true"/>
    <arg line="/home/munderwo/workspace/ShoutGen-Java/ShoutGen.conf"/>
</target>

When I try to do this and run it from within Eclipse I get

Buildfile: /home/munderwo/workspace/ShoutGen-Java/build.xml
run:
     [java] No config file passed as an argument. Please pass a configuration file
     [java] Java Result: 16

BUILD FAILED
/home/munderwo/workspace/ShoutGen-Java/build.xml:24: Problem: failed to create task or type arg
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

The error output from Java is my coded error meaning "you didn't pass an config file as an argument" which backs up the ant error of "Problem: failed to create task or type arg".

So how do you pass an argument to an executed JAR file from within Ant? Is this something your not supposed to do?

like image 465
Mark Lakewood Avatar asked Dec 27 '09 03:12

Mark Lakewood


People also ask

How do I pass a command line argument to a jar file?

We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …] As we can see, in this case, we'll have to include the main class name in the command line, followed by arguments.

How do I run an Ant script from the command line?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

What is Ant in command line?

To automate and simplify the above tasks, Apache Ant is useful. It is an Operating System build and deployment tool that can be executed from the command line.

What is ant jar?

JAR is a group of Java classes and known as Java Archive file. In Ant, we can create Jar files by using <jar> element in build. xml file.


2 Answers

The <arg> tag should be a child of the <java> tag. Like this:

<target name="run">
    <java jar="build/jar/ShoutGen.jar" fork="true">
        <arg line="/home/munderwo/workspace/ShoutGen-Java/ShoutGen.conf"/>
    </java>
</target>

In your question <arg> is a sibling of <java> and the argument line is never passed to the java command.

like image 106
Asaph Avatar answered Oct 05 '22 07:10

Asaph


Your arg statement is not properly nested in the java task. It needs to be

<java jar="...">
  <arg line="..." />
</java>
like image 34
Ajay Avatar answered Oct 05 '22 07:10

Ajay