Search code examples
javacgccjava-native-interfacenative

JNI: Problems compiling 64-bit native libraries


I compiled a native DLL file on Windows (64-bit) with GCC flags -c and -o Name.dll.

When I did System.load("fullpahhere"), I get this error:

java.lang.UnsatisfiedLinkError: RenderControl.dll: %1 is not a valid Win32 application

If I recompile adding a blank main() method to the C source and removing the -c flag, it then fails with an UnsatisfiedLinkError that says it can't load a 32 bit DLL on a 64 bit machine.

Why is Java calling a DLL with no main method invalid? Doesn't that ruin the whole point of DLL files and the JNI?

Update

I fixed the main() issue. This GCC invocation setup works:

gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I "C:\Program Files\Java\jdk1.6.0_32\include" -I "C:\Program Files\Java\jdk1.6.0_32\include\win32" -shared *.c -o lib.dll

I still need to figure out the JVM Architecture problem, however.

Where can I find a MinGW installation that is capable of compilnig 64 bit code? It seems my standard MinGW install only does 32 bit.

If I can get my hands on that, how do I decide which library to load in Java? Is there a System property that shows the JVM arch (NOT the OS arch)?


Solution

  • If you use the MinGW32 compiler, you cannot produce 64-bit code. This is something that is most likely intentional. Similar issues with other people I quickly looked up seem to suggest that you're trying to use a 32-bit native binary with 64-bit java.

    Try compiling with mingw-w64 and see if that allays the problem.

    There's also the possibility that your library simply isn't in the library path and that it's not being found even though you're giving System.load the full path, in which case you should make sure that your DLL is in the correct location for native libraries.