Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Native Interface 32 bit dll on 64 bit system

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?

like image 621
Frank Vanbever Avatar asked Mar 18 '12 10:03

Frank Vanbever


People also ask

How do I change a 32 bit DLL to 64 bit?

Windows CAN NOT load a 32bit dll into a 64bit process - this is a limitation that you can not circumvent. This means that if your 32bit DLL does any P/Invokes to other 32bit DLLS (or uses any 32bit . Net DLLS) you will be entirely out of luck (you will need to run the entire website in 32bit).

Can 64 bit Java load 32 bit DLL?

On 64-bit Windows, a 64-bit process cannot load a 32-bit dynamic-link library (DLL). Additionally, a 32-bit process cannot load a 64-bit DLL.

Can I run 32 bit Java on 64 bit Windows?

However you cannot run 64bits program if your OS and IE belong to a 32bit architecture. Also you will not be able to install programs belonging to both bits on the same laptop. This will make the 32bit Java run but will not work for 64. Was this reply helpful?


2 Answers

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); 
like image 158
Java42 Avatar answered Sep 28 '22 00:09

Java42


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

like image 36
fudge Avatar answered Sep 28 '22 00:09

fudge