JNA 示例程序 java.lang.NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1773720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JNA example program java.lang.NoClassDefFoundError
提问by Daryl Spitzer
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. (I downloaded jna.jar and put it in the same directory as HelloWorld.java for now.)
...使用javac -classpath .:jna.jar -g HelloWorld.java
没有错误。(我下载了 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.access0(URLClassLoader.java:56)
at java.net.URLClassLoader.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?
我如何让它运行?
采纳答案by ChssPly76
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 (e.g. folder where "com" is located):
为了正确编译/运行它,您需要从“根”文件夹(例如“com”所在的文件夹)运行 javac/java:
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
和 源代码都放在其中保留文件夹结构:
/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.
注意前一种情况下的路径分隔符和后一种情况下的点。
回答by jitter
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 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.
最好选择第二个选项,因为您迟早(可能更早)将不得不处理包裹。所以现在就花点时间阅读一下吧。
回答by Sergio
Here is a good example (in Spanish), http://bdevmex.blogspot.mx/2013/01/comunicar-aplicaciones-mediante-jna.htmlI hope that this can help you
这是一个很好的例子(西班牙语),http://bdevmex.blogspot.mx/2013/01/comunicar-aplicaciones-mediante-jna.html我希望这可以帮助你
回答by user2408280
In Eclipse, under Java Build path > Order and export
, select export jna.jar
.
在 Eclipse 下Java Build path > Order and export
,选择export jna.jar
。