How can I use Linux shared libraries in Java?
The answer is "JNI" :)
Here are a couple of links:
How to compile dynamic library for a JNI application on linux?
http://learn-from-the-guru.blogspot.com/2007/12/java-native-interface-jni-tutorial-hell.html
http://docs.oracle.com/javase/6/docs/technotes/guides/jni/
How to link shared library to another shared library
Let's decode your symbol:
$ c++filt _ZN5vatps6PosAPI8sendDataB5cxx11Ev
vatps::PosAPI::sendData[abi:cxx11]()
So, your code expects sendData
with std::string
with C++11 ABI, whereas libPosAPI.so
provides sendData
with pre-C++11 ABI std::string
.
abi:cxx11
hints to GCC5 and the C++11 ABI:
Users that depend on third-party libraries or plugin interfaces that still use the old ABI can build their code with
-D_GLIBCXX_USE_CXX11_ABI=0
and everything should work fine. In most cases, it will be obvious when this flag is needed because of errors from the linker complaining about unresolved symbols involving__cxx11
.
Java: load shared libraries with dependencies
OK;
I have found an acceptable solution in the end, but not without significant amount of hoops. What I do is
- Use the normal JNA mechanism to map the dlopen() function from the dynamic linking library (libdl.so).
- Use the dlopen() function mapped in with JNA to load external libraries "ext1" and "ext2" with the option RTLD_GLOBAL set.
It actually seems to work :-)
Shared library on Linux does not contain reference to one of its dependencies
Your version of cc
(or the link editor used by it) seem to default to -Wl,--as-neeeded
. In this case, the command line order matters. If -lcpdf
comes first, there are no references to its symbols yet, and so no dependency is created. -l
arguments should come last:
cc -shared -fpic -I$JAVA_HOME/include I$JAVA_HOME/include/linux jcpdfwrapper.c -o libjcpdf.so -L. -lcpdf
Java program that access C++ shared library that uses OpenCV
According to the Makefiles in your gist, the problem seems to be that the pkg-config --libs --cflags opencv
, which adds a bunch of -l library
arguments to the linker, is mentioned before the objects that actually depend on those libraries. In your Makefile, the easiest fix would be to move your $(INCLUDES)
reference to the end of the commands, after $(LIBS)
.
Related Topics
Understanding Metaspace Line in Jvm Heap Printout
What's the Location of the Javafx Runtime Jar File, Jfxrt.Jar, on Linux
Tool for Creating a Java Daemon Service on Linux
Java Is Installed, in Listing, But Execution Produces "./Java: No Such File or Directory"
Java Jsch Changing User on Remote MAChine and Execute Command
Tomcat 7: How to Set Initial Heap Size Correctly
Difference Between Using Java.Library.Path and Ld_Library_Path
How to Get the Ip of the Computer on Linux Through Java
How to Kill a Linux Process in Java with Sigkill Process.Destroy() Does Sigterm
Cannot Load R Xlsx Package on MAC Os 10.11
Wait Until Tomcat Finishes Starting Up
What Would Cause a Java Process to Greatly Exceed the Xmx or Xss Limit
Select Random Document from Firestore
How to Open a Command Terminal in Linux
Remote Debugging a Java Application
Why Do I Get "Exception; Must Be Caught or Declared to Be Thrown" When I Try to Compile My Java Code
Exception in Thread "Main" Java.Lang.Unsatisfiedlinkerror: No Lwjgl in Java.Library.Path