0

This is how I load the library.

static {
        try {
            System.load("/usr/lib/libname.a");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

When I choose .so (shared-library) application loads library without any error.
But there is a problem with loading static library.

While running the application exception happens :

Caused by: java.lang.UnsatisfiedLinkError: /usr/lib/libname.a: /usr/lib/libname.a: invalid ELF header (Possible cause: endianness mismatch)
    at java.lang.ClassLoader$NativeLibrary.load(Native Method) ~[na:1.8.0_242]
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1946) ~[na:1.8.0_242]
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1828) ~[na:1.8.0_242]
    at java.lang.Runtime.load0(Runtime.java:810) ~[na:1.8.0_242]
    at java.lang.System.load(System.java:1088) ~[na:1.8.0_242]

Codes runs on Linux machine.

alizeyn
  • 2,300
  • 1
  • 20
  • 30
  • Can you name any language where you can dynamically link a static library? – Elliott Frisch Feb 26 '20 at 12:18
  • You are loading the library at run time. .a libraries are not meant to be loaded at run time. – zomega Feb 26 '20 at 12:24
  • @somega So how can I load them? I read it's possible from java 8. – alizeyn Feb 26 '20 at 13:08
  • @ElliottFrisch https://stackoverflow.com/a/24544960/5629011 – alizeyn Feb 26 '20 at 13:09
  • First do a `file /usr/lib/libname.a` and make sure the library is correct for your platform. That link suggests your current code *should* work. But [this](https://stackoverflow.com/questions/24493337/linking-static-library-with-jni/24544960#comment63976119_24493825) ***suggests*** otherwise. – Elliott Frisch Feb 26 '20 at 13:15
  • @ElliottFrisch `libvalhalla.a: current ar archive` – alizeyn Feb 26 '20 at 13:21
  • @ElliottFrisch I don't have that problem with Java library, my error is `invalid ELF header` – alizeyn Feb 26 '20 at 13:40
  • 1
    Today you get to learn what the "l" means in [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format). – Elliott Frisch Feb 26 '20 at 13:41
  • @ElliottFrisch That's actually nice ;) – alizeyn Feb 26 '20 at 13:42
  • Calling System.load() is not enough to load a static library. As described [here](https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#compiling_loading_and_linking_native_methods), the library and the VM have to be combined. One way I can think of is to download the JVM source code and link your library into it. – zomega Feb 26 '20 at 13:43
  • Of course you could also wrap the .a so you will get a .so which you can load. – zomega Feb 26 '20 at 14:06

0 Answers0