Java Native Interface 32 bit dll on 64 bit system

2019-01-11 06:16发布

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 dll jni mingw
6条回答
唯我独甜
2楼-- · 2019-01-11 06:48
  1. Download mingw-w64.
  2. Update your Environment variable PATH.
  3. Create a C program named test.c which has implementation for your method.
  4. 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

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-11 06:53

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.

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

查看更多
一夜七次
4楼-- · 2019-01-11 07:00

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.

But there are workarounds like WOW64, which makes it possible. Please check out these links(1,2)

查看更多
老娘就宠你
5楼-- · 2019-01-11 07:05

You'll have to install a 32bit JVM and you will be able to run your code.

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.

    String archDataModel = System.getProperty("sun.arch.data.model");
    System.loadLibrary(libraryName+archDataModel);
查看更多
Melony?
6楼-- · 2019-01-11 07:05

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.

http://en.wikipedia.org/wiki/Itanium

查看更多
Juvenile、少年°
7楼-- · 2019-01-11 07:11

I got that same error message (without the stacktrace) after installing the Java plugin for the Chrome browser.

Re-installing JDK/JRE (this is a development environment) fixed it for me.

查看更多
登录 后发表回答