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?
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.
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.
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.
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.
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.
Your arg statement is not properly nested in the java task. It needs to be
<java jar="...">
<arg line="..." />
</java>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With