Java – ant buildfile cannot put the jar file into the classpath when running other jar files

I'm new to ant build files. I've managed to build my build files to create my build directory structure, compile all my files, and load them into a jar with the main class correctly specified in the manifest

However, I also tried to add the ability to run the jar I just created in the buildfile It will try to run, but I encountered 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 has the correct structure and the manifest is set correctly, but it can't find the class lab31 because it's not contained in that particular jar file Instead, it is contained in another jar file in my Library folder/ lib/. I tried to set the classpath to run the jar file in the same way as I set the classpath for the actual compilation, but it didn't seem to work

This is what I have for the running part of the build file:

<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 idea is that the jar file thinks resources The jar file is located at ${build} / jar / ${lib} / resources Jar, not just ${lib} / resources Jar, but if this is, I'm still not sure how to solve it Can anyone provide some guidance?

Solution

The behavior of Java tasks may be the same as that of the Java command line When the jar and classpath options are given, the classpath is ignored, and the jar should contain a list of the required libraries I suggest removing the jar attribute, specifying the two jars as classpath elements, and adding the classname attribute to the Java task with the main class name

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>