Java 在ant中设置classpath的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1575220/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:24:19  来源:igfitidea点击:

Problems with setting the classpath in ant

javaantclasspath

提问by David

I'm having problems getting my Java program to run (it uses some third party JARs). I can compile it fine but when I call my run target in ant it says it can't find the class that I told it run in the classpath. Here's what my build.xml looks like:

我在运行 Java 程序时遇到问题(它使用某些第三方 JAR)。我可以很好地编译它,但是当我在 ant 中调用我的运行目标时,它说它找不到我告诉它在类路径中运行的类。这是我的 build.xml 的样子:

<project basedir="." default="build">
<property name="build" value="build" />
<property name="src" value="." />
<property name="lib" value="lib" />

<path id="classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar" />
    </fileset>
    <fileset dir="${build}">
        <include name="*.class" />
    </fileset>
</path>

<target name="build">
    <javac srcdir="${src}" destdir="${build}">
        <classpath refid="classpath" />
    </javac>
</target>

<target name="run">
    <java classname="FirstClass">
        <classpath refid="classpath" />
    </java>
</target>

Does anyone know what I might be doing wrong?

有谁知道我可能做错了什么?



Here's my stack trace from ant:

这是我来自 ant 的堆栈跟踪:

ant run Buildfile: build.xml

run:
[java] Could not find GuiStarter. Make sure you have it in your classpath
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:138)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:764)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:218)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:132)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:105)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:616)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.Target.execute(Target.java:357)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:385)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
[java] at org.apache.tools.ant.Main.runBuild(Main.java:758)
[java] at org.apache.tools.ant.Main.startAnt(Main.java:217)
[java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
[java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
[java] Java Result: -1

BUILD SUCCESSFUL Total time: 1 second

回答by Bostone

The way it is - only jars inside libdirectory relative to your path are getting loaded. Do you have all of your 3-rd party jars there? There may be some run-time libraries that you don't need at compile time but which are missing at run time. If you post your stacktrace I'll tell you more :)

它的方式 - 只有lib相对于您的路径的目录中的jars才会被加载。你有你所有的第 3 方罐子吗?可能有一些您在编译时不需要但在运行时丢失的运行时库。如果您发布堆栈跟踪,我会告诉您更多信息:)

回答by Alexander Pogrebnyak

I think the problem is with your classpathpath declaration. The builddirectory should be a <pathelement>

我认为问题在于您的classpath路径声明。该build目录应该是一个<pathelement>

<path id="classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar" />
    </fileset>
    <pathelement location="${build}" />
</path>

Also, I would only include 3-rd party jars in your classpathrefid. So the whole block looks like.

另外,我只会在您的classpathrefid 中包含 3-rd party jars 。所以整个块看起来像。

<path id="3rd-party-classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar" />
    </fileset>
</path>

<target name="build">
    <javac srcdir="${src}" destdir="${build}">
        <classpath refid="3rd-party-classpath" />
    </javac>
</target>

<target name="run">
    <java classname="FirstClass">
      <classpath>
        <pathelement location="${build}" />
        <path refid="3rd-party-classpath" />
      </classpath>
    </java>
</target>

Also, as DroidIn.net has pointed out, you should create a package for you program.

此外,正如 DroidIn.net 所指出的,您应该为您的程序创建一个包。

回答by sathiya

Try this , i could able to run my class.

试试这个,我可以运行我的课程。

<property name="src.dir" value="src" />
<property name="build.dir" value="build" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="jar.dir" value="${build.dir}/jar" />
<property name="main-class" value="sample.Sample" />

<target name="clean">
    <delete dir="${classes.dir}" />
</target>

<path id="classpath">
    <fileset dir="${jar.dir}">
        <include name="*.jar" />
    </fileset>
    <fileset dir="${classes.dir}">
        <include name="/*.class" />
    </fileset>
</path>

<target name="compile">
    <mkdir dir="${classes.dir}" />
    <javac srcdir="${src.dir}" destdir="${classes.dir}">
        <classpath refid="classpath" />
    </javac>
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}" />
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}" />
        </manifest>
    </jar>
</target>

<target name="run" depends="jar">
    <java classname="${main-class}">
        <classpath refid="classpath" />
    </java>
</target>

<target name="clean-build" depends="clean,jar" />

<target name="main" depends="clean,run" />