简体   繁体   English

从jar运行时NoClassDefFoundError

[英]NoClassDefFoundError while running from jar

I am getting a "no class definition found" exception while trying to run my application on Windows (it runs fine on OS X). 我试图在Windows上运行我的应用程序时遇到“没有找到类定义”异常(它在OS X上运行正常)。 The classes the JVM is complaining about are my classes (no third party jars required). JVM抱怨的类是我的类(不需要第三方jar)。 When I unzip the files inside the jar, all files are present, including the ones the JVMm is complaining about. 当我解压缩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. 如果我解压缩jar并从我解压缩类的目录中运行jar,一切正常。 So, I am assuming all the required files are inside the jar. 所以,我假设所有必需的文件都在jar中。

EDIT: com.hamza.driver.ui is a class in a package called com.hamza.driver which 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.jar in 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$1.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 ClipboardTransferHandle as a static variable in the driver, it works, but every object that is not static is not found. 在玩它时,我发现如果我尝试将ClipboardTransferHandle声明为驱动程序中的静态变量,它可以工作,但是找不到每个非静态的对象。 All the main GUI elements are static variables, so the GUI is constructed, but other elements are not; 所有主要的GUI元素都是静态变量,因此构造了GUI,但其他元素却没有; everything that is created not static causes NoClassDefFound , but if I declare them static for testing, they work. 创建的所有内容都不是静态导致NoClassDefFound ,但如果我将它们声明为静态以进行测试,则它们可以正常工作。

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". 如果JAR文件是从“C:\\ java \\ apps \\ appli.jar”加载的,并且您的清单文件具有Class-Path:引用“lib / other.jar”,则类加载器将查看“C:\\ java” \\ apps \\ lib \\“for”other.jar“。 It won't look at the JAR file entry "lib/other.jar". 它不会查看JAR文件条目“lib / other.jar”。

Solution:- 解:-

  1. Right click on project, Select Export. 右键单击项目,选择“导出”。
  2. Select Java Folder and in it select Runnable JAR File instead of JAR file. 选择Java Folder并在其中选择Runnable JAR File而不是JAR文件。
  3. Select the proper options and in the Library Handling section select the 3rd option ie (Copy required libraries into a sub-folder next to the generated JAR). 选择正确的选项,然后在Library Handling部分中选择第3个选项,即(将所需的库复制到生成的JAR旁边的子文件夹中)。
  4. Click finish and your JAR is created at the specified position along with a folder that contains the JARS mentioned in the manifest file. 单击“完成”,将在指定位置创建JAR以及包含清单文件中提到的JARS的文件夹。
  5. open the terminal,give the proper path to your jar and run it using this command java -jar abc.jar 打开终端,为你的jar提供正确的路径并使用这个命令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. 现在将会发生的事情是类加载器将在引用的JARS的正确文件夹中查找,因为现在它们存在于包含应用程序JAR的同一文件夹中。现在没有抛出“java.lang.NoClassDefFoundError”异常。

    This worked for me... Hope it works you too!!! 这对我有用...希望它对你有用!!!

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是一个类还是一个包?

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. 我有一次这个问题,当一个类从另一个jar(在我的情况下是一个OSGi包)中导入了另一个没有正确导出的类。 Although this was a OSGi specific problem - You may have the same problems in your environment. 虽然这是OSGi特定的问题 - 您的环境可能会遇到同样的问题。 Maybe your application depends on some classes that are present in your actual OS-X environment but not in the actual Window environment. 也许您的应用程序依赖于实际OS-X环境中存在的某些类,而不是实际的Window环境中的类。 I'm not looking at third-party libraries but at the Java implementations itself. 我不是在看第三方库,而是在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 运行jar时NoClassDefFound

NoClassDefFoundError inside jar jar里面的NoClassDefFoundError

Edit 2 编辑2

Here's a similiar problem that has an 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 ...出了什么问题?

您是否在类路径中指定了新jar(java -cp .; new.jar MainClass.class)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM