7

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".

quimnuss
  • 1,503
  • 2
  • 17
  • 37
  • Check http://stackoverflow.com/questions/2349093/static-libraries-and-jni – BackSlash Mar 15 '13 at 13:31
  • @Harlandraka I saw the question before posting, but the link is dead. Also, now it's directly question-answer instead of question-comment. – quimnuss Mar 15 '13 at 13:42

4 Answers4

3

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

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Just like I thought... So I guess I'll have to recompile the static library using -fPIC flag, damn. – quimnuss Mar 15 '13 at 13:39
  • Or wrap it in a dynamic/shared library, avoiding the need to recompile. – Andy Thomas Mar 15 '13 at 13:44
  • Could you paste a link, how is it possible to get a shared library from a static library that hasn't been compiled with fPIC? – quimnuss Mar 15 '13 at 15:29
  • There's the specific question unanswered: http://stackoverflow.com/questions/13818921/link-static-library-without-fpic-to-shared-library-on-64bit-os – quimnuss Mar 15 '13 at 15:31
  • @quimnuss - Sorry, glossed over the -fPIC. Sometimes you can link static libraries into the dynamic/shared library. But with static libraries built without -fPIC, yes, looks like you have to recompile. Here's a link: http://stackoverflow.com/questions/12989334/shared-library-linked-with-static-library-relocation-error – Andy Thomas Mar 15 '13 at 16:15
  • I'm going back to this, how do you wrap a static library with a shared one? I'm working on it with CMake but I can't get it to work. – quimnuss Apr 09 '13 at 12:42
  • In general, include the static library in the linking of the shared one. Here's a specific case: http://stackoverflow.com/questions/2649735/how-to-link-static-library-into-dynamic-library-in-gcc . – Andy Thomas Apr 09 '13 at 13:20
  • I got it working but I have a doubt. The code will be packed within a jar file, and later extracted and used in some other computer. I hope I'm wrong, but, does the jarfile have to contain both libraries in order for it to find the symbols at run-time? @Andy – quimnuss Apr 10 '13 at 14:30
  • When you've linked the static library into the shared library, it's in the shared library. BTW, if you're using OSGi, it can do the extraction for you, if specified in the manifest with Bundle-NativeCode. – Andy Thomas Apr 10 '13 at 15:07
  • 1
    Starting with Java 8, the library may as well be statically linked with JVM, but I guess that means you would have to compile the JVM from source, in order to include your library that can be later loaded using System.loadLibrary() without needing a .dll / .so – xorcus Jun 18 '14 at 20:08
2

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

Andrii
  • 31
  • 2
1

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

msam
  • 4,259
  • 3
  • 19
  • 32
1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483