bgroenks
bgroenks

Reputation: 1889

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

Upvotes: 1

Views: 4437

Answers (2)

Scott Izu
Scott Izu

Reputation: 2309

Check out http://tdm-gcc.tdragon.net/, to download TDM-GCC which gives an easy installation on Windows for MinGW-w64 referenced above. You can use the following code to generate a 64-bit dll after installint TDM-GCC.

"C:\MinGW64\bin\gcc.exe" -m64 -c -I"C:\Program Files\Java\jdk1.6.0_26\include" -I"C:\Program Files\Java\jdk1.6.0_26\include\win32" HelloWorld.c 

It is the "-m64" which makes it 64 bit. This is run from the same directory as the HelloWorld.c C Code Source File. This generates the HelloWorld.o C Code Object File in the same directory as run..

Upvotes: 1

user308323
user308323

Reputation:

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.

Upvotes: 1

Related Questions