在 ant 中设置类路径时出现问题

2022-09-02 22:33:41

我在运行 Java 程序时遇到问题(它使用了一些第三方 JAR)。我可以很好地编译它,但是当我在ant中调用我的运行目标时,它说它找不到我告诉它在类路径中运行的类。这是我的构建.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>

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


这是我从蚂蚁的堆栈跟踪:

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

答案 1

我认为问题在于你的路径声明。该目录应为classpathbuild<pathelement>

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

另外,我只会在你的refid中包含3rd派对罐子。所以整个块看起来像这样。classpath

<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>

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


答案 2

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


推荐