简体   繁体   English

JNA示例程序java.lang.NoClassDefFoundError

[英]JNA example program java.lang.NoClassDefFoundError

I can compile this JNA example code (from step 2 of https://github.com/twall/jna/#getting_started ): 我可以编译这个JNA示例代码(来自https://github.com/twall/jna/#getting_started的第2步):

package com.sun.jna.examples;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {

    // This is the standard, stable way of mapping, which supports extensive
    // customization and mapping of Java to native types.
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)
            Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
                               CLibrary.class);

        void printf(String format, Object... args);
    }

    public static void main(String[] args) {
        CLibrary.INSTANCE.printf("Hello, World\n");
        for (int i=0;i < args.length;i++) {
            CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
        }
    }
}

...using javac -classpath .:jna.jar -g HelloWorld.java without error. ...使用javac -classpath .:jna.jar -g HelloWorld.java没有错误。 (I downloaded jna.jar and put it in the same directory as HelloWorld.java for now.) (我现在下载了jna.jar并将它放在与HelloWorld.java相同的目录中。)

But when I run it using java -classpath .:jna.jar HelloWorld , I get: 但是当我使用java -classpath .:jna.jar HelloWorld运行它时,我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: com/sun/jna/examples/HelloWorld)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

I get the exact same exception on Mac OS X and Linux. 我在Mac OS X和Linux上得到完全相同的例外。

How do I get this to run? 我如何让它运行?

This example (as well as vast majority of java classes) uses packages: 这个例子(以及绝大多数java类)使用包:

package com.sun.jna.examples;

In order to compile / run it properly you need to run javac / java from the "root" folder (eg folder where "com" is located): 为了正确编译/运行它,你需要从“root”文件夹运行javac / java(例如“com”所在的文件夹):

Let's say you have a folder called examples . 假设您有一个名为examples的文件夹。 You'd put both the jna.jar and the source code in it preserving folder structure : 您将jna.jar和源代码都放在其中保留文件夹结构

/examples
 jna.jar
 /com
   /sun
      /jna
         /examples
           HelloWorld.java

You compile and run using: 您编译并运行使用:

javac -classpath .:jna.jar -g com/sun/jna/examples/HelloWorld.java

java -classpath .:jna.jar com.sun.jna.examples.HelloWorld

Note the path separators in the former case and dots in the latter. 注意前一种情况下的路径分隔符和后一种情况中的点。

Either just remove this line and recompile (which is fine in this case as you just try out some sample) 要么只是删除这一行并重新编译(在这种情况下这很好,因为你只是尝试一些样本)

package com.sun.jna.examples;

or read up on what packages in java are and how they have to be handled (ChssPly76s Posts as a starter). 或者阅读java中的软件包以及如何处理它们(ChssPly76s帖子作为入门者)。

Better choose the second option as sooner or later (probably sooner) you will have to deal with packages anyway. 最好尽快选择第二个选项(可能更早),无论如何都要处理包裹。 So just take the time now to read up on it. 所以现在花点时间阅读它。

这是一个很好的例子(西班牙文), http://bdevmex.blogspot.mx/2013/01/comunicar-aplicaciones-mediante-jna.html我希望这可以帮到你

在Eclipse中,在Java Build path > Order and export ,选择export jna.jar

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

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