java 从 jar 运行时出现 NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1420119/
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
NoClassDefFoundError while running from jar
提问by Hamza Yerlikaya
I am getting a "no class definition found" exception while trying to run my application on Windows (it runs fine on OS X). The classes the JVM is complaining about are my classes (no third party jars required). When I unzip the files inside the jar, all files are present, including the ones the JVMm is complaining about.
尝试在 Windows 上运行我的应用程序时出现“未找到类定义”异常(它在 OS X 上运行良好)。JVM 抱怨的类是我的类(不需要第三方 jar)。当我解压缩 jar 中的文件时,所有文件都存在,包括 JVMm 抱怨的文件。
The jar is created using the following task:
jar 是使用以下任务创建的:
<target name="jar" depends="">
<jar destfile="build/app.jar" >
<manifest>
<attribute name="Built-By" value="hamza"/>
<attribute name="Main-Class" value="com.hamza.driver.ui"/>
<attribute name="Class-Path" value="./"/>
</manifest>
<fileset dir="build">
<include name="**/*.class"/>
<include name="**/*.png"/>
<include name="**/*.xpi"/>
<include name="**/*.html"/>
<exclude name="**/*.jar"/>
</fileset>
</jar>
I cannot figure out what is causing the problem. If I unzip the jar and run the jar from the directory I unzipped the class to, everything works fine. So, I am assuming all the required files are inside the jar.
我无法弄清楚是什么导致了问题。如果我解压缩 jar 并从我将类解压缩到的目录中运行 jar,一切正常。所以,我假设所有必需的文件都在 jar 中。
EDIT: com.hamza.driver.uiis a class in a package called com.hamza.driverwhich has main.
编辑:com.hamza.driver.ui是一个名为的包中的类,com.hamza.driver它具有main.
After the build, I get one jar "app.jar", and I run it using "java -jar app.jar", which executes fine on OS X, but not on Windows.
构建后,我得到一个 jar“app.jar”,我使用“java -jar app.jar”运行它,它在 OS X 上运行良好,但在 Windows 上运行不正常。
If I unzip app.jarin a seperate directory and run "java -jar app.jar", it excutes fine.
如果我app.jar在单独的目录中解压缩并运行“java -jar app.jar”,它会很好地执行。
EDIT 2: exception:
编辑2:例外:
Exception in thread "main" java.lang.NoClassDefFoundError: com/hamza/gui/tr
ansfer/ClipboardTransferHandle
at com.hamza.driver.ui.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.hamza.gui.transfer.Clipboa
rdTransferHandle
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 1 more
ClipboardTransferHandle .class files are present in the jar.
ClipboardTransferHandle .class 文件存在于 jar 中。
EDIT 3: imports for the clip board class:
编辑 3:剪贴板类的导入:
import java.util.logging.Logger; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.Toolkit; import java.io.IOException;
While playing with it, I found that if I try to declare ClipboardTransferHandleas a static variable in the driver, it works, but every object that is not static is not found.
All the main GUI elements are static variables, so the GUI is constructed, but other elements are not; everything that is created not static causes NoClassDefFound, but if I declare them static for testing, they work.
在玩它时,我发现如果我尝试ClipboardTransferHandle在驱动程序中声明为静态变量,它可以工作,但找不到每个非静态对象。所有主要的GUI元素都是静态变量,所以GUI被构造了,但其他元素不是;创建的所有内容都不是静态原因NoClassDefFound,但是如果我将它们声明为静态以进行测试,它们就可以工作。
回答by Gautam Mandsorwale
This is the problem that is occurring,
这是正在发生的问题,
if the JAR file was loaded from "C:\java\apps\appli.jar", and your manifest file has the Class-Path: reference "lib/other.jar", the class loader will look in "C:\java\apps\lib\" for "other.jar". It won't look at the JAR file entry "lib/other.jar".
如果 JAR 文件是从“C:\java\apps\appli.jar”加载的,并且您的清单文件具有 Class-Path: 引用“lib/other.jar”,则类加载器将在“C:\java \apps\lib\" 表示 "other.jar"。它不会查看 JAR 文件条目“lib/other.jar”。
Solution:-
解决方案:-
- Right click on project, Select Export.
- Select Java Folder and in it select Runnable JAR File instead of JAR file.
- Select the proper options and in the Library Handling section select the 3rd option i.e. (Copy required libraries into a sub-folder next to the generated JAR).
- Click finish and your JAR is created at the specified position along with a folder that contains the JARS mentioned in the manifest file.
open the terminal,give the proper path to your jar and run it using this command java -jar abc.jar
Now what will happen is the class loader will look in the correct folder for the referenced JARS since now they are present in the same folder that contains your app JAR..There is no "java.lang.NoClassDefFoundError" exception thrown now.
This worked for me... Hope it works you too!!!
- 右键单击项目,选择导出。
- 选择 Java 文件夹并在其中选择 Runnable JAR File 而不是 JAR 文件。
- 选择适当的选项,然后在库处理部分选择第三个选项,即(将所需的库复制到生成的 JAR 旁边的子文件夹中)。
- 单击完成,您的 JAR 将在指定位置创建,并创建一个包含清单文件中提到的 JARS 的文件夹。
打开终端,为您的 jar 提供正确的路径并使用此命令运行它 java -jar abc.jar
现在会发生什么是类加载器将在正确的文件夹中查找引用的 JARS,因为现在它们存在于包含您的应用程序 JAR 的同一文件夹中..现在没有抛出“java.lang.NoClassDefFoundError”异常。
这对我有用......希望它对你也有用!!!
回答by Dominic Cooney
Which class is missing? Your Main-Class attribute looks a little suspect--is com.hamza.driver.ui a class or a package?
缺哪个课?您的 Main-Class 属性看起来有点可疑——com.hamza.driver.ui 是一个类还是一个包?
回答by Andreas Dolk
There is a chance, that the NoClassDefFoundError (I really hate this error - always drives me crazy...) isn't thrown because it doesn't find the class it tells you (-> your class) but because java can't find one of the classes that are used to instantiate that class.
有可能,NoClassDefFoundError(我真的很讨厌这个错误 - 总是让我发疯......)没有被抛出,因为它没有找到它告诉你的类(->你的类)但是因为java不能找到用于实例化该类的类之一。
I had this problem once, when a class imported another class from a different jar (in my case: an OSGi bundle) which hadn't been properly exported. Although this was a OSGi specific problem - You may have the same problems in your environment. Maybe your application depends on some classes that are present in your actual OS-X environment but not in the actual Window environment. I'm not looking at third-party libraries but at the Java implementations itself.
我曾经遇到过这个问题,当一个类从一个没有正确导出的不同 jar(在我的例子中:一个 OSGi 包)导入另一个类时。尽管这是一个 OSGi 特定问题 - 您的环境中可能存在相同的问题。也许您的应用程序依赖于一些存在于您的实际 OS-X 环境中但不存在于实际 Window 环境中的类。我不是在看第三方库,而是在看 Java 实现本身。
Good luck!
祝你好运!
Edit
编辑
There are two more quite similar question on SO, unfortunately with no accepted solution, but maybe one of the hints over there can help in your case:
关于 SO 还有两个非常相似的问题,不幸的是没有公认的解决方案,但也许那里的提示之一可以帮助您解决问题:
NoClassDefFound when running a jar
NoClassDefFoundError inside jar
Edit 2
编辑 2
Here's a similiar problem that hasan accepted answer. Hope this one helps:
这是一个类似的问题,有一个公认的答案。希望这个有帮助:
NoClassDefFoundError while trying to run my jar with java.exe -jar...what's wrong?
尝试使用 java.exe -jar 运行我的 jar 时出现 NoClassDefFoundError ......出了什么问题?
回答by bwobbones
Have you specified the new jar in your classpath (java -cp .;new.jar MainClass.class)?
您是否在类路径中指定了新 jar (java -cp .;new.jar MainClass.class)?

