问题描述
我可以编译这个JNA示例代码(来自):
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]);
}
}
}
...使用 javac -classpath。:jna.jar -g HelloWorld.java
没有错误。 (我现在下载了jna.jar并将它放在与HelloWorld.java相同的目录中。)
...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.)
但是当我使用 java运行它时 - classpath。:jna.jar HelloWorld
,我得到:
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)
我在Mac OS X和Linux上得到完全相同的异常。
I get the exact same exception on Mac OS X and Linux.
如何让它运行?
推荐答案
这个例子(以及绝大多数java类)使用套餐:
This example (as well as vast majority of java classes) uses packages:
package com.sun.jna.examples;
为了正确编译/运行它,你需要从root文件夹运行javac / java (例如com所在的文件夹):
In order to compile / run it properly you need to run javac / java from the "root" folder (e.g. folder where "com" is located):
假设您有一个名为 examples
的文件夹。您将 jna.jar
和源代码都放在其中保留文件夹结构:
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.
这篇关于JNA示例程序java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!