如何在 ant 文件中设置类路径以执行 Java 程序并包含外部 jars?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10077620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the classpath in an ant file to execute a Java program and include external jars?
提问by Maltiriel
I've found examples on how to either set the classpath so that a Java program can be executed or to include external jars, but not both. My attempts to combine multiple examples in one file have failed.
我找到了有关如何设置类路径以便可以执行 Java 程序或包含外部 jar 的示例,但不能同时包含两者。我在一个文件中组合多个示例的尝试失败了。
My source .java files are in the src directory, the file I want to execute is src/TEDI.java, the class files get put correctly into the build directory, and all my jars are in the directory jung2-2_0_1. I can get my program to compile, but not execute, using the following ant file:
我的源 .java 文件在 src 目录中,我要执行的文件是 src/TEDI.java,类文件被正确放入构建目录中,我所有的 jars 都在目录 jung2-2_0_1 中。我可以使用以下 ant 文件编译我的程序,但不能执行:
<?xml version="1.0"?>
<project name="TEDI" basedir="." default="execute">
<property name="src" value="src"/>
<property name="output" value="build"/>
<property name="lib" value="jung2-2_0_1"/>
<target name="execute" depends="compile">
<echo>
Executing TEDI.
</echo>
<java classname="${output}/TEDI.class">
<classpath refid="java"/>
</java>
</target>
<target name="compile" depends="create">
<echo>
Compiling source files.
</echo>
<javac destdir="${output}">
<src path="${src}"/>
<classpath refid="java"/>
</javac>
</target>
<target name="clean">
<echo>
Deleting old class files.
</echo>
<delete dir="${output}"/>
</target>
<target name="create" depends="clean">
<echo>
Creating output directory.
</echo>
<mkdir dir="${output}"/>
</target>
<path id="java">
<pathelement location="${output}"/>
<fileset dir="${lib}">
<include name="*.jar"/>
</fileset>
</path>
</project>
When I run ant, it does the clean, create, and compile targets just fine, and then when it gets to execute it says: Could not find build/TEDI.class. Make sure you have it in your classpath
当我运行 ant 时,它会很好地执行清理、创建和编译目标,然后当它执行时它说: Could not find build/TEDI.class. Make sure you have it in your classpath
Anyway I'm hoping someone can tell me what I'm doing wrong in the path
section. I added the pathelement
bit after reading one example on how to create an ant target to execute a file, but it didn't help at all. There are a ton of examples and a lot of them do things differently to achieve the same thing (while none do exactly what I'm trying to do), so I can't figure out which way is correct for what I'm trying to do. Any help or ideas would be greatly appreciated.
无论如何,我希望有人能告诉我我在该path
部分做错了什么。pathelement
在阅读了一个关于如何创建 ant 目标来执行文件的示例后,我添加了这一点,但它根本没有帮助。有大量的例子,其中很多以不同的方式做事来实现同样的事情(虽然没有一个完全按照我正在做的事情做),所以我无法弄清楚哪种方式适合我正在尝试的事情去做。任何帮助或想法将不胜感激。
Edit: Changed <pathelement location="${build}"/>
to <pathelement location="${output}"/>
as per Sandro's answer, but it doesn't change the error message at all.
编辑:改变<pathelement location="${build}"/>
以<pathelement location="${output}"/>
按桑德罗的答案,但它并不会改变该错误消息。
采纳答案by Dan Berindei
The java
task expects a Java class name, not the path of a class file.
So you should use
该java
任务需要 Java 类名,而不是类文件的路径。所以你应该使用
<java classname="TEDI">
instead of
代替
<java classname="${output}/TEDI.class">
回答by Sandro
As the class you want to execute seems to be in ${output}
, you have to include ${output}
in your classpath.
由于您要执行的类似乎在 中${output}
,因此您必须将其包含${output}
在类路径中。
Try adding <pathelement location="${output}"/>
to your path.
尝试添加<pathelement location="${output}"/>
到您的路径。