Java的运行NoClassDefFoundError的罐子包含库JAR时 [英] Java NoClassDefFoundError when running jar containing library jar

查看:348
本文介绍了Java的运行NoClassDefFoundError的罐子包含库JAR时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时后,我在我束手无策,甚至阅读的文档和SO问题里姆斯后。我敢肯定,我失去了一些东西很明显,但我无法弄清楚。

After several hours, I'm at my wit's end, even after reading reams of documentation and SO questions. I'm certain that I'm missing something obvious, but I just can't figure it out.

我创建了一些java文件,包括main方法的单一入口点。这个类也利用一库级,位于com.test.lib.MyLibraryClass.class,一个jar文件中,mylib.jar的。我建立的我的的成功使用下面的蚂蚁XML。jar文件

I've created a number of java files, including a single entry point with a main method. That class also makes use of one "library" class, located in com.test.lib.MyLibraryClass.class, within a jar file, mylib.jar. I'm building my jar file successfully using the following ant XML.

<target name="jar" depends="compile">
        <jar destfile="${jar.dir}/${jar.name}.jar">
            <fileset dir="${classes.dir}" />
            <fileset dir="${lib.dir}" />
            <manifest>
                <attribute name="Main-Class" value="${main-class}" />
                <attribute name="Class-Path" value="mylib.jar"/>
            </manifest>
        </jar>
    </target>

当我检查通过执行这一目标创造了罐子,我看到它确实包含了我所有的.class文件以及mylib.jar。

When I inspect the jar created by executing that target, I see that it does contain all of my .class files as well as mylib.jar.

当我尝试然而,运行jar,我得到以下错误:

When I try to run the jar however, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/test/lib/MyLibraryClass
    at com.mytest.MyMain.<init>(Unknown Source)
    at com.mytest.MyMain.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.test.lib.MyLibraryClass
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 2 more

我需要做什么改变?我是不是产生一个畸形的或不完整的清单?

What do I need to change? Am I generating a malformed or incomplete manifest?

感谢你了!

推荐答案

的问题是,标准的类加载器不能找到在一个JAR是另一个JAR中的类。在类路径清单变量的真正的告诉JVM添加 mylib.jar 文件在当前目录到classpath。

The problem is that the standard classloader cannot find classes that are in a JAR that is inside another JAR. The Class-Path manifest variable is actually telling the JVM to add the mylib.jar file in the current directory to the classpath.

有三种解决方法:


  • mylib.jar 文件的地方,可以发现(并设置相应清单属性。

  • Put the mylib.jar file somewhere that it can be found (and set the manifest property accordingly.

创建尤伯杯-JAR,结合类在主JAR和所有相关的库JAR到一个JAR文件。

Create an Uber-jar that combines the classes in your main JAR and all of the relevant library JARs into one JAR file.

写一个时髦的类装载器,它知道如何从JAR-IN-A-JAR加载和修改实例,并使用类加载器的应用程序。 (不建议使用此方法...)

Write a funky class loader that knows how to load from a JAR-in-a-JAR, and modify the application to instantiate and use the classloader. (This approach is NOT recommended ...)

请注意,前两个替代方案通过摆脱你的JAR-在一个-JAR结构...以不同的方式解决这个问题。

Note that the first two alternatives solve the problem by getting rid of your JAR-in-a-JAR structure ... in different ways.

这篇关于Java的运行NoClassDefFoundError的罐子包含库JAR时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆