32
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?

Frank Vanbever
  • 427
  • 2
  • 6
  • 11
  • Can you tell the JRE/JDK and OS version you are using right now (32/64-bit; I suspect you are using Windows)? For more info: https://forums.oracle.com/forums/thread.jspa?threadID=2271763 – ecle Mar 18 '12 at 10:12
  • Link help you.http://stackoverflow.com/questions/8374193/possible-to-force-a-64-bit-jvm-to-use-32-bit-mode-via-the-argument-d32 – 4b0 Mar 18 '12 at 10:13
  • i'm using windows 7 ultimate x86_64 and the x86 version of jdk1.7.0_03 – Frank Vanbever Mar 18 '12 at 10:39
  • 1
    To load a 32-bit DLL, you have to do it from a 32-bit application e.g. 32-bit Java. You can run 32-bit application on a 64-bit OS, but not the other way around. – Peter Lawrey Mar 18 '12 at 10:53
  • ok i ran java -version and apparently changing the folders in the path enviroment variable wasn't enough. i used the adress of the 32-bit java.exe instead of just the java command and it worked – Frank Vanbever Mar 18 '12 at 11:56

6 Answers6

30

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);
Java42
  • 7,628
  • 1
  • 32
  • 50
  • That probably wont help as the JVM does not run the DLL. Its the OS which does it. JVM simply delegates the call to DLL. – Santosh Mar 18 '12 at 10:23
  • 5
    I don't agree. I build 32bit DLLs and run a 32bit JVM and all is fine on a 64bit OS. Also, his trace shows java.lang.System.loadLibrary so the JVM is loading the dll. The JNI DLL 32 vs 64 needs to match the JVM 32 vs 64. – Java42 Mar 18 '12 at 10:34
8

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

fudge
  • 121
  • 1
  • 5
  • 1
    `Error: This Java instance does not support a 32-bit JVM.` –  Apr 01 '16 at 19:34
  • 1
    [This Oracle FAQ](http://www.oracle.com/technetwork/java/hotspotfaq-138619.html#64bit_layering) suggests the -d32/-d64 option is only useful on Solaris; on Windows/Linux you need to install the appropriate 32 or 64 bit JVM and specifically run the one you want (e.g. by including it in your system path). – Ian Renton Jan 10 '17 at 09:04
1

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.

isapir
  • 21,295
  • 13
  • 115
  • 116
1
  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

VAr
  • 2,551
  • 1
  • 27
  • 40
arun kumar
  • 511
  • 7
  • 10
-2

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

-2

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)

Community
  • 1
  • 1
Santosh
  • 17,667
  • 4
  • 54
  • 79
  • 1
    This is not true. One can execute a 32-bit JVM on a 64-bit OS. The _caller_ of the DLL determines whether it can be loaded. You are correct on whether the JVM itself can run as a 32-bit program. Which it can. A 64-bit binary cannot load a 32-bit DLL. This is the fundamental issue at play here. – Andrew T Finnell Jul 03 '18 at 15:57