运行其他 jar 文件时,Ant buildfile 无法将 jar 文件放入类路径中
我是 Ant 构建文件的新手,我已经设法设置我的构建文件来创建我的构建目录结构,编译我的所有文件,并将它们打包到一个包含正确指定主类的清单的 jar 中。
但是,我还尝试将运行我刚刚创建的 jar 的功能放入构建文件中。它将尝试运行,但我遇到此错误:
run:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: edu/course/lab/pkg3/Lab31
[java] at edu.school.oad.lab.pkg1.Main.<init>(Unknown Source)
[java] at edu.school.oad.lab.pkg1.Main.main(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: edu.course.antlab.pkg3.Lab31
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[java] ... 2 more
[java] Java Result: 1
所以我知道这意味着当我尝试运行 jar 文件时,jar 文件的结构正确并且清单设置正确,但它找不到类 Lab31 因为它不包含在该特定 jar 文件中。相反,它包含在我的库文件夹 ./lib/ 中的另一个 jar 文件中。我尝试使用与实际编译设置类路径相同的方法来设置运行 jar 文件的类路径,但这似乎不起作用。
这是我对构建文件的运行部分的内容:
<target name="run" depends="init, prepare">
<java jar="${build}/jar/AntLabRun.jar" fork="true">
<classpath>
<pathelement location="${lib}/resources.jar" />
</classpath>
</java>
</target>
我唯一的想法是 jar 文件认为 resources.jar 文件位于 ${build}/jar/${lib}/resources.jar 而不仅仅是 $ {lib}/resources.jar,但如果是这种情况,我仍然不确定如何修复它。有人可以提供一些指导吗?
I'm new to Ant buildfiles, and I've managed to set up my buildfile to create my build directory structure, compile all my files, and jar them into an jar with a manifest specifying the main class correctly.
However, I'm also trying to put into the buildfile the capability to run the jar I just created. It will attempt to run, but I encounter this error:
run:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: edu/course/lab/pkg3/Lab31
[java] at edu.school.oad.lab.pkg1.Main.<init>(Unknown Source)
[java] at edu.school.oad.lab.pkg1.Main.main(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: edu.course.antlab.pkg3.Lab31
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[java] ... 2 more
[java] Java Result: 1
So I know this means that when I try to run the jar file, the jar file is structured properly and the manifest is set up correctly, but it cannot find the class Lab31 because it is not included in that particular jar file. Instead, it is contained within another jar file located in my library folder, ./lib/ . I tried using the same method to set the classpath for running the jar file the same way that I set the classpath for the actual compilation, but that doesn't seem to be working.
Here's what I have for the run portion of the buildfile:
<target name="run" depends="init, prepare">
<java jar="${build}/jar/AntLabRun.jar" fork="true">
<classpath>
<pathelement location="${lib}/resources.jar" />
</classpath>
</java>
</target>
My only thought would be that the jar file is thinking the resources.jar file is located at ${build}/jar/${lib}/resources.jar instead of just ${lib}/resources.jar, but if that's the case I'm still not sure how to fix it. Can anyone offer some guidance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
java 任务的行为方式可能与 java 命令行相同。当同时给出 jar 和 classpath 选项时,类路径将被忽略,而 jar 应该包含列出所需库的清单。我建议删除 jar 属性,将两个 jar 指定为类路径元素,并将
classname
属性添加到具有主类名称的 java 任务中。The java task probably behaves the same way as the java command line. When both jar and classpath options are given, the classpath is ignored, instead the jar is expected to contain a manifest listing the needed libraries. I would suggest removing the jar attribute, specifying both jars as classpath elements and adding the
classname
attribute to the java task with the name of the main class.或者,您也可以将类路径(即 resources.jar)的 jar 添加到 jar 清单的
Class-Path
属性中。然后你的 jar 也可以在 ant 之外使用-jar
运行。我在构建文件中有这样的内容:
如果您的路径中只有一个文件,则不需要
,但可以直接将其添加到<属性>
。不过,该路径必须相对于主 jar 文件的位置。Alternatively, you also could add the jars of your class path (i.e. resources.jar) into the
Class-Path
attribute of the jar manifest. Then your jar is also runnable with-jar
outside of ant.I have something like this in the build file:
If you only have one file in your path, you don't need the
<pathconvert>
, but can add it directly in the<attribute>
. The path has to be relative from the location of your main jar file, though.