JNA 示例程序 java.lang.NoClassDefFoundError

发布于 2024-08-12 17:59:20 字数 2211 浏览 7 评论 0原文

我可以编译此 JNA 示例代码(来自 https://github.com/twall/jna 的步骤 2 /#getting_started):

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]);
        }
    }
}

...使用 javac -classpath .:jna.jar -g HelloWorld.java 没有错误。 (我下载了 jna.jar 并将其放在与 HelloWorld.java 相同的目录中。)

但是当我使用 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)

: Mac OS X 和 Linux 上完全相同的异常。

我如何让它运行?

I can compile this JNA example code (from step 2 of https://github.com/twall/jna/#getting_started):

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. (I downloaded jna.jar and put it in the same directory as HelloWorld.java for now.)

But when I run it using java -classpath .:jna.jar HelloWorld, I get:

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.

How do I get this to run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-08-19 17:59:20

此示例(以及绝大多数 java 类)使用包:

package com.sun.jna.examples;

为了正确编译/运行它,您需要从“root”文件夹(例如“com”所在的文件夹)运行 javac / java:

假设您有一个名为 examples 的文件夹。您可以将 jna.jar 和源代码放入其中保留文件夹结构

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

您可以使用以下命令进行编译和运行:

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

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

注意前一种情况中的路径分隔符和中的点后者。

This example (as well as vast majority of java classes) uses packages:

package com.sun.jna.examples;

In order to compile / run it properly you need to run javac / java from the "root" folder (e.g. folder where "com" is located):

Let's say you have a folder called examples. You'd put both the jna.jar and the source code in it preserving folder structure:

/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.

鲜肉鲜肉永远不皱 2024-08-19 17:59:20

要么删除这一行并重新编译(在本例中这很好,因为您只是尝试一些示例),

package com.sun.jna.examples;

要么阅读 java 中的包是什么以及如何处理它们(ChssPly76s 帖子作为初学者)。

最好选择第二个选项,因为迟早(可能更快)您无论如何都必须处理包裹。所以现在就花点时间来阅读一下吧。

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).

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.

很快妥协 2024-08-19 17:59:20

在 Eclipse 中,在 Java Build path > 下订购并导出,选择导出jna.jar

In Eclipse, under Java Build path > Order and export, select export jna.jar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文