Why Java does not see a native library on Linux? - java

I have a problem including a native library to my Java project on Linux Ubuntu.
A directory /mnt contains files treedb.jar and jkyotocabinet.so which are needed for launching:
root#vtt9:/mnt# ls -l
-rwxr-xr-x 1 root root 1948919 Mar 11 2014 jkyotocabinet.so
-rwxr-xr-x 1 root root 1404322 Nov 18 16:11 treedb.jar
I start the program with param -Djava.library.path=.:
root#vtt9:/mnt# sudo java -Xms5000m -Xmx51000m -d64 -jar
-Djava.library.path=. treedb.jar
The program prints strings to the console:
current dir = /mnt/.
java library path = .
Then I have an exception on System.loadLibrary("jkyotocabinet");:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jkyotocabinet in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at treedb.Main.main(Main.java:27)
Also I looked in java -XshowSettings:properties, put jkyotocabinet.so to another directories in java.library.path, set programmatically System.setProperty( "java.library.path", "." );
I don't understand what is wrong with it? On Windows it works great. I need only replace .so to .dll.

Related

JNA undefined symbol on Ubuntu 20.04

I am attempting to load a native library using JNA, using this line:
MPV INSTANCE = Native.load("mpv", MPV.class, Map.of(Library.OPTION_STRING_ENCODING, "UTF-8"));
This works absolutely fine on an Ubuntu 20.04 that I installed in a Hyper-V VM on my main machine, but for some reason fails on an Ubuntu 20.04 native install.
The logs where it fails look like this:
Looking in classpath from {0} for {1}
Found library resource at {0}
Extracting library to {0}
Trying {0}
Found jnidispatch at {0}
Looking for library 'mpv'
Adding paths from jna.library.path: null
Trying libmpv.so
Loading failed with message: libmpv.so: cannot open shared object file: No such file or directory
Adding system paths: [/usr/lib/x86_64-linux-gnu, /lib/x86_64-linux-gnu, /usr/lib64, /lib64, /usr/lib, /lib, /lib/i386-linux-gnu, /usr/lib/x86_64-linux-gnu/libfakeroot]
Trying libmpv.so
Loading failed with message: libmpv.so: cannot open shared object file: No such file or directory
Looking for version variants
Trying /usr/lib/x86_64-linux-gnu/libmpv.so.1.107.0
Loading failed with message: /lib/x86_64-linux-gnu/libavfilter.so.7: undefined symbol: av_sscanf, version LIBAVUTIL_56
Looking in classpath from {0} for {1}
Loading failed with message: Native library (linux-x86-64/libmpv.so) not found in resource path ([file:/home/xxx/yyy/mpv.jar])
Exception in thread "JavaFX Application Thread"
java.lang.UnsatisfiedLinkError: Unable to load library 'mpv':
libmpv.so: cannot open shared object file: No such file or directory
libmpv.so: cannot open shared object file: No such file or directory
/lib/x86_64-linux-gnu/libavfilter.so.7: undefined symbol: av_sscanf, version LIBAVUTIL_56
Native library (linux-x86-64/libmpv.so) not found in resource path ([file:/home/xxx/yyy/mpv.jar])
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:302)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:455)
at com.sun.jna.Library$Handler.<init>(Library.java:192)
at com.sun.jna.Native.load(Native.java:596)
at hs.mediasystem.ext.mpv.MPV.<clinit>(MPV.java:12)
at hs.mediasystem.ext.mpv.MPVPlayer.<init>(MPVPlayer.java:47)
(... snipped 50 lines ...)
at com.sun.glass.ui.View.handleKeyEvent(View.java:547)
at com.sun.glass.ui.View.notifyKey(View.java:971)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
at java.base/java.lang.Thread.run(Thread.java:834)
Suppressed: java.lang.UnsatisfiedLinkError: libmpv.so: cannot open shared object file: No such file or directory
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:191)
... 70 more
Suppressed: java.lang.UnsatisfiedLinkError: libmpv.so: cannot open shared object file: No such file or directory
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:204)
... 70 more
Suppressed: java.lang.UnsatisfiedLinkError: /lib/x86_64-linux-gnu/libavfilter.so.7: undefined symbol: av_sscanf, version LIBAVUTIL_56
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:235)
... 70 more
Suppressed: java.io.IOException: Native library (linux-x86-64/libmpv.so) not found in resource path ([file:/home/john/Downloads/MediaSystem/ui-plugins/mpv-plugin/mpv.jar])
at com.sun.jna.Native.extractFromResourcePath(Native.java:1095)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:276)
... 70 more
For some reason it cannot find some symbol av_sscanf in a dependent library. The wierd thing however is that this exact same code works in the Ubuntu installed in the VM.
Its output is:
Looking in classpath from {0} for {1}
Found library resource at {0}
Extracting library to {0}
Trying {0}
Found jnidispatch at {0}
Looking for library 'mpv'
Adding paths from jna.library.path: null
Trying libmpv.so
Loading failed with message: libmpv.so: cannot open shared object file: No such file or directory
Adding system paths: [/usr/lib/x86_64-linux-gnu, /lib/x86_64-linux-gnu, /usr/lib64, /lib64, /usr/lib, /lib]
Trying libmpv.so
Loading failed with message: libmpv.so: cannot open shared object file: No such file or directory
Looking for version variants
Trying /usr/lib/x86_64-linux-gnu/libmpv.so.1.107.0
Found library 'mpv' at /usr/lib/x86_64-linux-gnu/libmpv.so.1.107.0
I checked the versions and sizes of the libraries involved, and they seem to be all exactly the same:
john#Artec:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04 LTS
Release: 20.04
Codename: focal
john#Artec:~$ apt-cache policy libmpv1
libmpv1:
Installed: 0.32.0-1ubuntu1
Candidate: 0.32.0-1ubuntu1
Version table:
*** 0.32.0-1ubuntu1 500
500 http://nl.archive.ubuntu.com/ubuntu focal/universe amd64 Packages
100 /var/lib/dpkg/status
john#Artec:~$ apt-cache policy libavfilter7
libavfilter7:
Installed: 7:4.2.2-1ubuntu1
Candidate: 7:4.2.2-1ubuntu1
Version table:
*** 7:4.2.2-1ubuntu1 500
500 http://nl.archive.ubuntu.com/ubuntu focal/universe amd64 Packages
100 /var/lib/dpkg/status
john#Artec:~$ ll /usr/lib/x86_64-linux-gnu/libmpv.so.1.107.0
-rw-r--r-- 1 root root 2153952 jan 31 19:11 /usr/lib/x86_64-linux-gnu/libmpv.so.1.107.0
john#Artec:~$ ll /usr/lib/x86_64-linux-gnu/libavfilter.so.7
-rw-r--r-- 1 root root 3647472 mrt 7 09:39 /usr/lib/x86_64-linux-gnu/libavfilter.so.7.57.100
Any tips on how to debug this further would be much appreciated.
More Info
This turns out to be a problem where two libraries are loaded, but requiring different versions of a shared dependent library (in this case the libavfilter library is used by both opencv/ffmpeg and libmpv, but they need a slightly different version...).

JPL/SWI Prolog not working

I'm trying to use the SWI-Prolog JPL library, but I'm having problems.
I'm trying to let my Eclipse project access JPL, but when I try to start the program, I get the following error:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no jpl in java.library.path
I copied the jpl.jar into my project directory, and I set it on the build path. Additionally, I pass the following VM arguments:
-Djava.library.path="C:\Program Files\Prolog\bin"
(That's the directory where Prolog was installed).
What am I doing wrong?
EDIT: I don't seem to have the libpl.dll anywhere on my computer. Could this be causing my problem?
The jvm.dll of your running JDK/JRE must be available in your system PATH so that jpl.dll from java.library.path loads properly.
You have to take care to 32 bits / 64 bits consistency between your JPL installation and your running JVM - so do not try any mix.
Here is information from an old installation doc.
For such installation support, you should use the JPL mailing list.
You should set the java.library.path to the folder where the jpl.dll file is located. As far as I know it is the Prolog\bin folder.
A system-wide solution in a Mac environment (SWI-Prolog version 7.1.4 for x86_64-darwin13.1.0) would be to create the following symlink:
ln -s /usr/local/Cellar/swi-prolog/7.1.4/libexec/lib/swipl-7.1.4/lib/x86_64-darwin13.1.0/libjpl.jnilib /Library/Java/Extensions/libjpl.jnilib
Could be rather
ln -s
/Applications/SWI-Prolog.app/Contents/swipl/lib/x86_64-darwin/libjpl.dylib
/Library/Java/Extensions/libjpl.dylib
? (no .inilib was found under darwin folder)
In my case still error on mojave (SWI-Prolog (threaded, 64 bits, version 8.0.2)):
Jan 03, 2020 12:10:55 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [ACTIONS] in context with path [/SVIZ] threw exception [Servlet execution threw an exception] with root cause
java.lang.UnsatisfiedLinkError: /Applications/SWI-Prolog.app/Contents/swipl/lib/x86_64-darwin/libjpl.dylib: dlopen(/Applications/SWI-Prolog.app/Contents/swipl/lib/x86_64-darwin/libjpl.dylib, 1): Library not loaded: #rpath/libswipl.8.dylib
Referenced from: /Applications/SWI-Prolog.app/Contents/swipl/lib/x86_64-darwin/libjpl.dylib
Reason: image not found

ClassNotFoundException: in gnu.gcj.runtime.SystemClassLoader

I am getting this Exception when i tried to run a java Application with the help of a Script
Caused by: java.lang.ClassNotFoundException: com.tata.topListQuoteSamples.TOPLISTSample not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:/home/sai/NewgdpapiToplist1/lib/commons-logging-1.0.4.jar,file:/home/sai/NewgdpapiToplist1/lib/log4j-1.2.8.jar,file:/home/sai/NewgdpapiToplist1/lib/mail.jar,file:/home/sai/NewgdpapiToplist1/lib/marketdata-b1.1.jar,file:/home/sai/NewgdpapiToplist1/lib/middleware.jar,file:/home/sai/NewgdpapiToplist1/lib/gdpapi-all.jar], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at gnu.java.lang.MainThread.run(libgcj.so.7rh)
End time: Fri Mar 9 09:15:55 EST 2012
Basically you need to set the class path straight inside your script.
if you're executing java with "java.exe" (or whatever the linux equivalent) make sure you specify the class path (or if you're running that from the command line, make sure you either do that or are in the right folder)

java javax.comm

Hi im trying to read com port, so I add library to my java directory like
they wrote here but when Im witing simple code like
import java.util.Enumeration;
import javax.comm.*;
public class CompotCore {
public static void main(String[] args)
{
Enumeration list = CommPortIdentifier.getPortIdentifiers();
}
}
I get such error:
java.lang.UnsatisfiedLinkError: no Serial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681)
at java.lang.Runtime.loadLibrary0(Runtime.java:840)
at java.lang.System.loadLibrary(System.java:1047)
at gnu.io.RXTXCommDriver.<clinit>(RXTXCommDriver.java)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at javax.comm.CommPortIdentifier.loadDriver(CommPortIdentifier.java:237)
at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:109)
at CompotCore.main(CompotCore.java:11)
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.sun.comm.SunrayInfo.isSessionActive()Z
at com.sun.comm.SunrayInfo.isSessionActive(Native Method)
at com.sun.comm.Portmapping.registerCommPorts(Portmapping.java:155)
at com.sun.comm.Portmapping.refreshPortDatabase(Portmapping.java:100)
at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:138)
at CompotCore.main(CompotCore.java:11)
i read that I had to add System.load("Serial"); with full path but I dont have such library and dont know where to find it.
On the site you link to it includes instructions to copy two native libraries into your JRE lib dir:
cp rxtx-bins.1/1.4/i386-pc-linux/libParallel.so /usr/java/j2sdk1.4.0/jre/lib/i386/
cp rxtx-bins.1/1.4/i386-pc-linux/libSerial.so /usr/java/j2sdk1.4.0/jre/lib/i386/
Are they there?
Be sure you are copying them into the correct JRE. What does which java tell you?
An alternative to copying to jre/lib would be to have them in some other location and then point to that dir with the System Property java.library.path, e.g. you would execute Java like this to start your program
java -Djava.library.path=<dir_with_those_libs> <your_other_args>
Update
OK I downloaded the rtxt tar and took a look. After you untar it, you have the following (one example):
ls -l rxtx-bins.1/1.4/i386-pc-linux/
total 44
-rwxr-xr-x 1 root root 9700 Dec 5 2001 libParallel-1.4.15.so
lrwxrwxrwx 1 root root 21 Jul 6 03:23 libParallel.so -> libParallel-1.4.15.so
-rwxr-xr-x 1 root root 31400 Dec 5 2001 libSerial-1.4.15.so
lrwxrwxrwx 1 root root 19 Jul 6 03:23 libSerial.so -> libSerial-1.4.15.so
If you followed the instructions from that page, you will have only copied the links, not the actual libs. So the solution will be to copy ALL the files, something like this:
cp rxtx-bins.1/1.4/i386-pc-linux/* /usr/java/j2sdk1.4.0/jre/lib/i386/
Or alternatively to execute Java something like:
java -Djava.library.path=~/rxtx-bins.1/1.4/i386-pc-linux:$LD_LIBRARY_PATH <your_other_args>
The page that you linked to offers you to download the RXTX package. In that archive file there are the native libraries that you need for different platforms.
You just have to make sure that Java can find the relevant native library for the OS that you are using. You can do that by specifying the directory that contains the native library in the java.library.path system property, which you can set on the command line like this:
java -Djava.library.path=C:\Folder\That\Contains\TheDLL CompotCore
For Linux, obviously:
java -Djava.library.path=/folder/that/contains/the_so CompotCore
(Ofcourse, substitute the actual directory in the command).

LibraryNotFoundException while running java code that invokes dlls

I have written a code that requires 2 dll's at runtime.I have those dll files in C:\Program Files\Java\jdk1.6.0_01\jre\bin.
I have set the environment variable PATH accordingly.
It compiled successfully and then while running it gives the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at teamdev.jxcapture.Capture.captureActiveWindow(Unknown Source)
at ppb.activewindow.execute(activewindow.java:24)
at ppb.activewindow.main(activewindow.java:68)
Caused by: com.jniwrapper.LibraryNotFoundException: Cannot find JNIWrapper nativ
e library (jniwrap.dll) in java.library.path: C:\Program Files\Java\jdk1.6.0_01\
jre\bin\jniwrap.dll
Could anybody please help me to resolve this problem?
The value of java.library.path should be the directory containing the DLLs, not the filename of the DLLs themselves. It looks like your java.library.path is set to
C:\Program Files\Java\jdk1.6.0_01\jre\bin\jniwrap.dll
instead of
C:\Program Files\Java\jdk1.6.0_01\jre\bin
It looks like the DLL is not being found on the Java Library Path. See this - http://www.inonit.com/cygwin/jni/helloWorld/load.html
Try moving JDK to a path without spaces.

Categories