Search code examples
javalinuxjava-native-interfacestatic-libraries

JNI linking static library: Invalid ELF header


I am trying to make use of a static library using JNI. Therefore I am compiling my library statically g++ -c -std=c++11 -o jnitest_jnitest_App.o jnitest_jnitest_App.cpp -lOtherLib

and then package it

ar rcs jnitest_jnitest_App.a jnitest_jnitest_App.o

Now when I try to run my .jar I get the following error:

jnitest/jnitest_jnitest_App.a: invalid ELF header (Possible cause: endianness mismatch) at java.base/java.lang.ClassLoader$NativeLibrary.load0(Native Method) at java.base/java.lang.ClassLoader$NativeLibrary.load(ClassLoader.java:2424) at java.base/java.lang.ClassLoader$NativeLibrary.loadLibrary(ClassLoader.java:2481) at java.base/java.lang.ClassLoader.loadLibrary0(ClassLoader.java:2678) at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2611) at java.base/java.lang.Runtime.load0(Runtime.java:814) at java.base/java.lang.System.load(System.java:1838) at jnitest.jnitest.App.<clinit>(App.java:13)

I am using a 64-bit Linux Mint 19 and Java 10 (64 bit)

Compiling the native code into a shared library gives no error with JNI. Where is the problem here?


Solution

  • That documentation does not suggest that a static library can be loaded by loadLibrary, only a dynamic (a.k.a shared library) that has itself been statically linked with the VM, which it might well be. Dynamically loading a static library is impossible. See static-libraries