简体   繁体   English

尝试从jar实例化类时出现java.lang.NoClassDefFoundError

[英]java.lang.NoClassDefFoundError when trying to instantiate class from jar

I did found quite a lot about this error, but somehow none of the suggested solutions resolved the problem. 我确实发现了很多有关此错误的信息,但是无论如何,建议的解决方案都无法解决该问题。

I am trying to use JNA bindings for libgphoto2 under Ubuntu in Eclipse (moderate experience with Java on Eclipse, none whatsoever on Ubuntu, I'm afraid). 我正在尝试在Eclipse中的Ubuntu下为libgphoto2使用JNA绑定(恐怕对Eclipse上的Java有一定的经验,而在Ubuntu上则没有任何经验)。 The bindings in question I want to use are here: http://angryelectron.com/projects/libgphoto2-jna/ 我要使用的绑定问题在这里: http : //angryelectron.com/projects/libgphoto2-jna/

I followed the steps described on that page, and made a simple test client that failed with the above error. 我按照该页面上描述的步骤进行操作,并制作了一个简单的测试客户端,但由于上述错误而失败。 So I reduced the test client until the only thing I tried to do was to instantiate a GPhoto2 object, which still produced the error. 因此,我减少了测试客户端,直到我唯一尝试做的事情是实例化GPhoto2对象,该对象仍然产生错误。 The test client looks like this: 测试客户端如下所示:

import com.angryelectron.gphoto2.*;

public class test_class 
{
    public static void main(String[] args)
    {
        GPhoto2 cam = new GPhoto2();
    }

}

The errors I get take up considerably more space: 我得到的错误占用了更多的空间:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/Structure
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at test_class.main(test_class.java:12)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Structure
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 13 more

libgphoto2 itself is installed, it runs from the command line, I even have the development headers and am able to call GPhoto2 functions from python, so the problem can't be located there. libgphoto2本身是已安装的,它是从命令行运行的,我什至拥有开发标头,并且能够从python调用GPhoto2函数,因此无法在此处找到问题。

When looking at the .class files in Eclipse, however, they didn't have any definitions. 但是,在Eclipse中查看.class文件时,它们没有任何定义。 So I figured that might be the problem, especially since there was an error when building the whole thing with ant (although the .jar was succesfully exported, from what I could make out the error concerned only the generation of documentation). 因此,我认为这可能是问题所在,尤其是因为使用ant构建整个对象时出现了错误(尽管.jar已成功导出,但从我可以确定的错误中,我仅发现了文档的生成)。 So I loaded the source into eclipse and built the .jar myself. 因此,我将源代码加载到eclipse中并自己构建了.jar。 At this occasion Eclipse stated there were warnings during the build (though no errors), but didn't show me the actual warnings. 在这种情况下,Eclipse表示在构建期间有警告(尽管没有错误),但没有显示实际警告。 If anyone could tell me where the hell the build log went, that might already help something. 如果有人可以告诉我构建日志在哪里,那可能已经有所帮助。 I searched for it everywhere without success, and if I click on "details" in eclipse it merely tells me where the warnings occured, not what they were. 我到处搜索它都没有成功,如果我在日食中单击“详细信息”,它只会告诉我警告发生的位置,而不是警告的内容。

Be that as it may, a warning isn't necessarily devastating, so I imported the resulting Jar into the above client. 即便如此,警告也不一定具有破坏性,因此我将生成的Jar导入上述客户端。 I checked the .class files, this time they contained all the code. 我检查了.class文件,这次它们包含所有代码。 But I still get the exact same list of errors (yes, I have made very sure that the old library was removed from the classpath and the new ones added. I repeated the process several times, just in case). 但是我仍然得到完全相同的错误列表(是的,我已经确保从类路径中删除了旧库,并添加了新库。为了以防万一,我重复了几次该过程)。

Since I don't have experience with building jars, I made a small helloworld jar, just to see if I could call that from another program or if I'd be getting similar errors. 由于我没有构建jar的经验,因此制作了一个小的helloworld jar,以查看是否可以从另一个程序调用它,或者是否会遇到类似的错误。 It worked without a hitch. 它工作顺利。 I even tried to reproduce the problem deliberately by exporting it with various options, but it still worked. 我什至尝试通过使用各种选项将其导出来故意重现该问题,但是它仍然有效。 I tried re-exporting the library I actully need with the settings that had worked during my experiment, but they still wouldn't run. 我尝试使用实验期间可以使用的设置重新导出我确实需要的库,但是它们仍然无法运行。 I'm pretty much stuck by now. 现在我已经很困了。 Any hints that help me resolve the problem would be greatly appreciated. 任何帮助我解决问题的提示将不胜感激。

In addition to what @Paul Whelan has said. 除了@Paul Whelan所说的。 You might have better luck by just get the missing jar directly. 通过直接获取丢失的罐子,可能会带来更好的运气。

Get the missing library here , set the classpath and then re-run the application again and see whether it will run fine or not. 此处获取缺少的库,设置类路径,然后再次重新运行该应用程序,看看它是否可以正常运行。

What version of java are you using com/sun/jna/Structure may only work with certain JVMs. 您正在使用com / sun / jna / Structure的Java版本仅适用于某些JVM。

In general, packages such as sun. 一般来说,包装如防晒霜。 , that are outside of the Java platform, can be different across OS platforms (Solaris, Windows, Linux, Macintosh, etc.) and can change at any time without notice with SDK versions (1.2, 1.2.1, 1.2.3, etc). 不在Java平台上,并且在OS平台(Solaris,Windows,Linux,Macintosh等)上可能有所不同,并且可以随时更改,而对于SDK版本(1.2、1.2.1、1.2.3等),恕不另行通知)。 Programs that contain direct calls to the sun. 包含直接调用太阳的程序。 packages are not 100% Pure Java. 软件包不是100%纯Java。

More details here 在这里更多细节

Your jar needs a MANIFEST.MF which tells your application where the library is found. 您的jar需要一个MANIFEST.MF,它可以告诉您的应用程序在哪里找到该库。 Create the file in you project root-directory in eclipse and add the following lines: 在eclipse的项目根目录中创建文件,并添加以下行:

Manifest-Version: 1.0
Class-Path: <PATH_TO_LIB__CAN_BE_RELATIVE>.jar       // e.g Class-Path: ../test.jar
<empty line>

Right-click your project in eclipse, go to Export->next->next->next->Use existing manifest from workspace , select it and click on finish. 在eclipse中右键单击您的项目,转到Export-> next-> next-> next->使用工作区中的现有清单 ,选择它并单击完成。 This should work. 这应该工作。

Another solution is to compile the classes into the jar itself with Maven. 另一种解决方案是使用Maven将类编译到jar本身。

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

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