Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to link a static library to Java through JNI?

Is it possible to build a HelloWorld.lib and load it to a Java application using JNI? Or it just works with shared libraries?

I couldn't find a clear answer on the JNI documentation, there's no reference to "static library".

like image 941
quimnuss Avatar asked Mar 15 '13 13:03

quimnuss


People also ask

How static library is linked?

Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.

Does Java have static linking?

I was following a tutorial video on compilers on YouTube, when I came across the fact that the C Programming Language is faster because it allows both static linking and dynamic linking, but Java allows only dynamic linking and that is why C is much faster than Java.

What happens when you link a static library?

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.

What is JNI wrapper?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).


4 Answers

It needs to be a dynamic library. Fortunately, you can build a dynamic library from a static one.

like image 95
Andy Thomas Avatar answered Oct 04 '22 03:10

Andy Thomas


Java 8 supports statically linked native libraries http://openjdk.java.net/jeps/178

like image 20
Andrii Avatar answered Oct 04 '22 04:10

Andrii


To load a library at runtime it must be a dll (windows). If you have a static library (lib) and you have to use it via JNI you have to create a wrapper dll

like image 30
msam Avatar answered Oct 04 '22 03:10

msam


You would have to link it to the JVM, and you don't have a way to do that. That's why JNI is defined with shared libraries, not static ones.

like image 26
user207421 Avatar answered Oct 04 '22 03:10

user207421