How to add .so file to the java.library.path in Linux
Add the containing directory to LD_LIBRARY_PATH
before launching the application
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/some/pathOfContainingDirectory
Use java -XshowSettings:properties
to show the java.library.path (and others) value.
How to include a directory to java.library.path on Linux/Debian?
You can do this via adding the containing directory to LD_LIBRARY_PATH like
**
export LD_LIBRARY_PATH = $LD_LIBRARY_PATH:/Desktop/pathOfContaininglibaryDirectory
**
into your bashrc file.Then you need to refresh bashrc file via command
source ~/.bashrc
then run you can run the java command(java -XshowSettings:properties) to see all the properties it will come under user.dir
How could set the java.library.path in Linux from I am trying JNI
This seems like path issue, where your libhellojni.so is not found in "sybase/IQ-16_0/jim_samples/udf/hellojni" directory.
Remember 'lib' prefix is important in your xxx.so file (if you are running this in Linux), just hellojni.so will not work.
Loading a Linux .so File at Java Runtime
Libraries on Linux are often named in the pattern libXXX.so
, and I believe Java follows that convention. So System.loadLibrary("Sample")
may be looking for libSample.so
. You can verify this by making a quick test program to call System.mapLibraryName
and checking the output.
To resolve the issue, assuming this is in fact the problem you're having, you can either rename your library file or use System.load
(not System.loadLibrary
), which will load the library specified by the exact filename you pass it, without any transformations. The latter method is not portable across platforms, though.
Why java.library.path is not working on ubuntu?
So my question is why java.library.path is not works for ubuntu (linux)? Is it like java.library.path is only for windows?
It does work, we use it a lot. Start your application with -XshowSettings:properties
and take a look at the search path for debugging.
We usually deploy the libraries as a package to /usr/local/lib
, since the libraries are often used by other components too. Don't forget to call ldconfig
after placing a new library in there (so much for the export LD_LIBRARY_PATH
part).
As far as i remember it should be enough with just adding the folder with -Djava.library.path
if i recall correctly. I will look into it and tell you later to clarify.
Also please post readlink -f /home/ubuntu/Desktop/bin
, file /home/ubuntu/Desktop/bin/libcef.so
and ldd /home/ubuntu/Desktop/bin/libcef.so
.
Update:
I will try to explain why things work and why not.
Lets talk about java.library.path
. This property is used by the VM for looking up libraries. Take a look at java.lang.System#load*(String libName) for reference. The java.library.path
property has some paths pre-set, the following shows the output on my ubuntu box:
ortang@vbox-devel:~$ java -XshowSettings:properties
Property settings:
...
java.library.path = /usr/java/packages/lib/amd64
/usr/lib/x86_64-linux-gnu/jni
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/lib/jni
/lib
/usr/lib
Be aware that using this property will overwrite the existing property.
ortwin@vbox-devel:~$ java -Djava.library.path=/some/other/folder:/yet/another/one -XshowSettings:properties
Property settings:
...
java.library.path = /some/other/folder
/yet/another/one
So far so good. The JVM is looking only in the folders defined in that property!
The libraries you make the JVM to load will most likely have dependencies to other libraries. Be aware that these dependencies are looked up by the operating system, just as any other shared library!
So to solve your problem you have to make sure that the libraries you load have their dependencies resolved! Use ldd
for debugging that matter.
The LD_LIBRARY_PATH
environment variable does a similar job, as it adds paths that will be used for library lookup by the OS. I am no fan of using it in production environments.
Load .so file from a jar
First, we need to make sure the JAR file is in the class path. Then, here is a simple way of loading the library, assuming it is found under /com/example/libMYLIB.so
in the JAR:
InputStream is = ClassLoader.class.getResourceAsStream("/com/example/libMYLIB.so");
File file = File.createTempFile("lib", ".so");
OutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
is.close();
os.close();
System.load(file.getAbsolutePath());
file.deleteOnExit();
But this glosses over a lot of corner cases and portability issues that are covered by JavaCPP inside the Loader class.
Related Topics
Match a String That Contains a Newline Using Sed
Why Can't I Sys_Write from a Pointer to Stack Memory, Using Int 0X80
Symbolic Link: Find All Files That Link to This File
Cmake:Set Environment Variables from a Script
How to Convert Pptx Files to Jpg or Png (For Each Slide) on Linux
X Keypress/Release Events Capturing Irrespective of Window in Focus
How to Move a Relative Symbolic Link
Git Gc: No Space Left on Device, Even Though 3Gb Available and Tmp_Pack Only 16Mb
Fast String Search in a Very Large File
Asp.Net Core Environment Variable Colon in Linux
How to Output Return Code in Shell
Bash Script to Find The Frequency of Every Letter in a File
Define Function in Unix/Linux Command Line (E.G. Bash)
Removing Sensitive Data from Git. "Fatal: Ambiguous Argument 'Rm'"