problem with java task in Apache Ant
I am an Apache Ant novice and I would li开发者_开发技巧ke to create a build file with a run task. The run task should execute the following command line statemenet
java -classpath C:/tmp/SYS/doodle.jar;C:/tmp/SYS/CTX.jar sys.ctx.doodle.Start
where the sys.ctx.doodle.Start class is located in the doodle.jar
My question is: how can I add two elements in a classpath? I have tried the following:
<target name="run">
<java jar="C:/tmp/SYS/doodle.jar" fork="true">
<classpath>
<pathelement location="C:/tmp/SYS/doodle.jar"/>
<pathelement path="sys.ctx.doodle.Start"/>
</classpath>
<classpath>
<pathelement location="C:/tmp/SYS/CTX.jar"/>
</classpath>
</java>
</target>
But when executing it throws me a java.lang.NoClassDefFoundError
Any idea where the problem may be?
You can transpose your command-line path into the Ant java
task classpath attribute directly. Ant should take care of recognising that is composed of semicolon-delimited jar names.
<java classpath="C:/tmp/SYS/doodle.jar;C:/tmp/SYS/CTX.jar" ... >
Or you can specify it as a nested element as you currently have:
<java ... >
<classpath>
<pathelement path="C:/tmp/SYS/doodle.jar;C:/tmp/SYS/CTX.jar" />
</classpath>
</java>
The argument sys.ctx.doodle.Start
looks like the name of the class you want to run. Use the classname
attribute to pass that to the java
task. Putting that with the classpath leads to:
<java classpath="C:/tmp/SYS/doodle.jar;C:/tmp/SYS/CTX.jar"
classname="sys.ctx.doodle.Start" />
The jar
attribute should be used only when you want to run the Main-Class
included in that jar.
For adding multiple jars to your classpath reference using a Filesetseems to be a clean way to do it
example:
<classpath>
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</classpath>
Will add all jars inside the lib directory to the classpath.
You have too many <classpath>
elements. What you need is single <classpath>
element like this:
<classpath>
<pathelement location="C:/tmp/SYS/doodle.jar"/>
<pathelement location="C:/tmp/SYS/CTX.jar"/>
</classpath>
You will need to change the <java>
tag like this:
<java jar="C:/tmp/SYS/doodle.jar" fork="true" classname="sys.ctx.doodle.Start">
精彩评论