64 位系统上的 Java 本机接口 32 位 dll
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9757456/
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
Java Native Interface 32 bit dll on 64 bit system
提问by Frank Vanbever
E:\Code\Java\JNITest>java test
Exception in thread "main" java.lang.UnsatisfiedLinkError: E:\Code\Java\JNITest\test.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at test.main(test.java:16)`
While using Java Native Interface I ran into a problem that generated this error. I believe this is because I compiled the .dll with MinGW which compiles to a 32-bit .dll whilst my system is 64-bit and thus my Java runs at 64-bit. Is there anyway to force my Java to run at 32-bits?
在使用 Java Native Interface 时,我遇到了一个导致此错误的问题。我相信这是因为我用 MinGW 编译了 .dll,它编译为 32 位 .dll,而我的系统是 64 位,因此我的 Java 运行在 64 位。有没有办法强制我的 Java 以 32 位运行?
采纳答案by Java42
You'll have to install a 32bit JVM and you will be able to run your code.
您必须安装 32 位 JVM,然后才能运行您的代码。
If you are going to distribute your application, you will want to build both 32bit and 64bit versions of your DLL. Then use the following technique to have the proper DLL loaded regardless of your customers arch. Append either a 32 or a 64 (MyJniDLL32.dll & MyJniDLL64.dll) to your generated output file.
如果您要分发您的应用程序,您将需要构建 32 位和 64 位版本的 DLL。然后使用以下技术加载正确的 DLL,而不管您的客户架构如何。将 32 或 64(MyJniDLL32.dll 和 MyJniDLL64.dll)附加到生成的输出文件中。
String archDataModel = System.getProperty("sun.arch.data.model");
System.loadLibrary(libraryName+archDataModel);
回答by Santosh
The DLLs are run by the native OS. Java simply delegates the call to DLL which is very closely knit with the OS on which its compiled. In general you cannot do it in straightforwd way and here is way.
DLL 由本机操作系统运行。Java 只是将调用委托给与编译它的操作系统密切相关的 DLL。一般来说,你不能以直接的方式做到这一点,这里是 way。
But there are workarounds like WOW64, which makes it possible. Please check out these links(1,2)
回答by jjr8228
IA is Itanium architecture so a AMD jvm is trying to load a dll that was built for Itanium...don't think that will work.
IA 是 Itanium 架构,所以 AMD jvm 试图加载一个为 Itanium 构建的 dll ......不要认为这会起作用。
回答by isapir
I got that same error message (without the stacktrace) after installing the Java plugin for the Chrome browser.
在为 Chrome 浏览器安装 Java 插件后,我收到了同样的错误消息(没有堆栈跟踪)。
Re-installing JDK/JRE (this is a development environment) fixed it for me.
重新安装 JDK/JRE(这是一个开发环境)为我修复了它。
回答by fudge
Just to state the obvious: to load a native library built for a 32bit architecture, you have to force the JVM to start in 32bit mode.
简单说明一下:要加载为 32 位架构构建的本机库,您必须强制 JVM 以 32 位模式启动。
java -d32 ...
Possibly you need to install an older JVM for your platform (eg. Oracle's Java 7 on OS X is 64bit only, you need to get Apple's Java 6 from their knowledge base).
可能您需要为您的平台安装较旧的 JVM(例如,OS X 上的 Oracle 的 Java 7 只有 64 位,您需要从他们的知识库中获取 Apple 的 Java 6)。
回答by arun kumar
- Download mingw-w64.
- Update your Environment variable PATH.
- Create a C program named test.cwhich has implementation for your method.
Run the following cmd in Command prompt
gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o test.dll test.c
- 下载mingw-w64。
- 更新您的环境变量 PATH。
- 创建一个名为test.c的 C 程序,它具有您的方法的实现。
在命令提示符下运行以下 cmd
gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o test.dll test.c