Why does OpenCV throw an UnsatisfiedLinkError when using Mat? - java

When constructing a new Mat instance in OpenCV 3.2, the program throws an UnsatisfiedLinkError. This happens both when invoking the Mat constructor directly, as well as when calling Imgcodecs.imread, which in turn throws an error on the following line:
Mat mats_mat = Converters.vector_Mat_to_Mat(mats);
The program is being run on a Mac running the latest version of macOS Sierra (10.12.3), and all available dylib files are located in the java.library.path directory. The file being read has been confirmed to exist. The stack trace reads (partially) as follows:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.imgcodecs.Imgcodecs.imread_1(Ljava/lang/String;)J
at org.opencv.imgcodecs.Imgcodecs.imread_1(Native Method)
at org.opencv.imgcodecs.Imgcodecs.imread(Imgcodecs.java:118)
The dylib files are loaded using the following code in main with no errors or warnings:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.loadLibrary("opencv_imgproc.3.2.0");
System.loadLibrary("opencv_imgcodecs.3.2");
System.loadLibrary("opencv_videoio.3.2");
System.loadLibrary("opencv_highgui.3.2.0");
System.loadLibrary("opencv_flann.3.2");
System.loadLibrary("opencv_ml.3.2");
System.loadLibrary("opencv_features2d.3.2");
System.loadLibrary("opencv_calib3d.3.2");
System.loadLibrary("opencv_objdetect.3.2");
System.loadLibrary("opencv_photo.3.2");
System.loadLibrary("opencv_stitching.3.2");
System.loadLibrary("opencv_video.3.2.0");
System.loadLibrary("opencv_superres.3.2");
System.loadLibrary("opencv_shape.3.2");
System.loadLibrary("opencv_videostab.3.2");
The error is thrown when performing any of the following actions:
this(Imgcodecs.imread(filePath));
Mat mat = new Mat();
MatOfPoint matOfPoint = new MatOfPoint();

Printing out Core.NATIVE_LIBRARY_NAME on my machine (Ubuntu 16.04 w/ OpenCV 3.2 with Java and Python ports) gives me this:
opencv_java320
It seems to me that System.load requires a complete path whereas Core.NATIVE_LIBRARY_NAME only gives a partial name, not even including the extension! OpenCV Java has its default binary files path located at /usr/local/share/OpenCV/java. (At least for me or people who used the default settings when compiling) there you can find the library libopencv_java320.so which is the actual, correct native library you need to link to!

Related

Problem connecting Java and Prolog with JPL

I wanted to connect Java and Swi Prolog together using JPL.
When I added the library to my project on Intellij the code compiled and when I tried to run a query I got a runtime error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jpl in java.library.path: [C:\Program Files\Java\jdk-12\bin, C:\WINDOWS\Sun\Java\bin, C:\WINDOWS\system32, C:\WINDOWS, c:\swipl\bin, ${env_var:PATH}, .]
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2660)
at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:827)
at java.base/java.lang.System.loadLibrary(System.java:1902)
at org.jpl7.JPL.loadNativeLibrary(JPL.java:114)
at org.jpl7.fli.Prolog.<clinit>(Prolog.java:71)
at org.jpl7.Query.open(Query.java:369)
at org.jpl7.Term.textToTerm(Term.java:155)
at org.jpl7.Query.<init>(Query.java:169)
at Main.main(Main.java:7)
I have the swi prolog 64 bit.
I've tried uninstalling it and use the 32 bit but it did not work.
What I did so far:
I added SWI_HOME_DIR to my Environment Variables.
I also added the swi path to Path variable.
I added the jpl library to my project (and it added it successfully).
The code I was trying to run:
import org.jpl7.*;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Query q = new Query("true");
q.hasSolution();
Map<String,Term>[] res = q.allSolutions();
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
}
So, is jpl.dll in any of the listed directories:
C:\Program Files\Java\jdk-12\bin ... probably not
C:\WINDOWS\Sun\Java\bin ... probably not
C:\WINDOWS\system32 ... probably not
C:\WINDOWS ... probably not
c:\swipl\bin ... apparently yes as c:\swipl\bin\jpl.dll exists?
${env_var:PATH} ... apparently not
Try the suggestion from this question in your Java program:
File nativeFile = new File(filename + ".dll");
if (!nativeFile.exists())
System.exit(1);
System.load(nativeFile);
Note that just having jpl.jar is not enough. One needs the jpl.dll file, too. jpl.jar is good for the Java part of the Java-Prolog bridge, but to be able to call a non-JVM compilate, we need to get into system-level details, Hence the dll file.
See troubleshooting tips here: JPL Deploying for users - on Windows
From the above page:
If the Java examples complain that
The dynamic link library libpl.dll could not be found in the specified path
or
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\paul\bin\jpl.dll: Can't find dependent libraries
then there is no SWI-Prolog library libpl.dll in any folder on your
PATH: you should have a PATH entry such as C:\Program Files\pl\bin.
The libpl.dll should contain the code for SWI-Prolog itself.

Can't load OpenCV on Linux - undefined symbol error

So I would like to play with OpenCV little bit. My test project is in Java (OS is Debian Linux 8.4), and I have followed this tutorial to build OpenCV: https://opencv-java-tutorials.readthedocs.io/en/latest/01-installing-opencv-for-java.html
After fixing few issues, I was able to successfully build OpenCV jar and so file. There were no errors or warnings during the build. I have put opencv-400.jar and libopencv_java400.so into lib subfolder of my project. Added the jar file to build path in Eclipse, and put correct path to so file in Eclipse's Build Configurations.
My project has just a Main class which is a sample I found in OpenCV's sources, so nothing complicated:
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to OpenCV " + Core.VERSION);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("m = " + m.dump());
}
}
It all looks that it should be working fine, but when I run the project, I see this exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/firzen/ownCloud/develop/java/workspace/CVExperiments/lib/libopencv_java400.so: /home/firzen/ownCloud/develop/java/workspace/CVExperiments/lib/libopencv_java400.so: undefined symbol: _ZNK6google8protobuf8internal12MapFieldBase28SpaceUsedExcludingSelfNoLockEv
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1938)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1854)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at Main.main(Main.java:9)
And this part of exception is really making me worried:
/home/firzen/ownCloud/develop/java/workspace/CVExperiments/lib/libopencv_java400.so: undefined symbol: _ZNK6google8protobuf8internal12MapFieldBase28SpaceUsedExcludingSelfNoLockEv
It almost seems to me that there is something wrong with that libopencv_java400.so file. Am I right? Or do I need some another files to make it work? I have build OpenCV as Debug, so that so file has 135 MiB, but that shouldn't be a problem I think.
I will be thankful for any ideas!
That error means that your system does not have a suitable protobuf library installed. You might give sudo apt install libprotobuf10 a try, although I can't guarantee that will help.
Alternatively, you can also run ldd /home/firzen/ownCloud/develop/java/workspace/CVExperiments/lib/libopencv_java400.so, which will show you all libraries OpenCV tries to import - perhaps Protobuf has actually been built along with OpenCV, but is just not installed.

AIX: "java.lang.UnsatisfiedLinkError" but the library exists

I'm using JNI to call a shared library named "libmy_jni.so" from my java code. Those are working good without any problem in other OS's but in AIX, I see the following error. I've checked that the LIBPATH and LD_LIBRARY_PATH(in case) are set correctly and matched x86 module with 32bit java(and x64 with 64bit java).
Exception in thread "Thread-1" java.lang.UnsatisfiedLinkError: my_jni (No such file or directory)
The java code loading the library is,
File sofile = new File("libmy_jni.so");
if(!sofile.exists())
{
System.out.println("can't find the moudle.");
System.exit(1);
}
System.loadLibrary("my_jni");
As I know, the most cases causing the error above are incorrect library path or unmatched 32/64bit architecture. But in my case I don't see what could be the reason.
>>file libmy_jni.so
libmy_jni.so: 64-bit XCOFF executable or object module not stripped

Java JNI on Mac using Eclipse giving UnsatisfiedLinkError

I am using Eclipse Java and Eclipse C/C++ on OSX Yosemite to create a simple Java JNI application. Here are the steps I performed
Wrote a class on Java Eclipse with native method.
Using this class, created a C header file using javah executable and included the the header file in Eclipse C/C++. On building the C code, .so file is created.
Then I added the path to .so file using eclipse (Run/Run Configutations/Java Application/Environment) by setting variable name as PATH and value as %PATH%/Users/XXXX/Documents/JniWorkspace/JniExampleLibrary/Debug
Here is Java code to load the C library:
public class JniExample {
static {
System.loadLibrary("JniExampleLibrary");
}
public native void callNativeMethod();
....
....
}
But still I am getting the error as shown below:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no JniExampleLibrary in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1878)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1087)
at code.example.JniExample.(JniExample.java:5)
at code.example.Main.main(Main.java:8)

java UnsatisfiedLinkError awt.image

I have a program that makes use of the following method to get a scaled instance of an image icon:
public ImageIcon createScaledImageIcon(String filename) {
ImageIcon icon = new ImageIcon(filename);
Image image = icon.getImage().getScaledInstance(cardWidth, cardHeight, Image.SCALE_SMOOTH);
icon.setImage(image);
return icon;
}
I don't know if it's the source of the problem or not. But i get the following error messages:
Exception in thread "Image Fetcher 0" java.lang.UnsatisfiedLinkError: sun.awt.image.ImageRepresentation.setBytePixels(IIII[BIILsun/awt/image/ByteComponentRaster;I)V at sun.awt.image.ImageRepresentation.setBytePixels(Native Method)
at sun.awt.image.ImageRepresenation.setPixels(Unknown Source)
at sun.awt.image.ImageDecoder.setPixels(Unknown Source)
at sun.awt.image.GIFImageDecoder.sendPixels(Unknown Source) ...
Let me know if there is any other information I could include that might be of use.
Try reinstalling the latest version of Java. Your installation may have been incomplete/corrupt (read similar experience).
A user at this link suggests that this error may be the result of receiving a Java response and not rebooting. Whether "rebooting" referes to the entire computer, or just restarting java is unknown, though.
I had this same problem with a Squirrel SQL install on a Windows machine. It turned out that the cause was the environment setting for the PATH. I had set JAVA_HOME to point to the Java version I installed, but there was another version of Java in the path earlier than the one I had installed (which was sitting at the end of the path).
So my JAVA_HOME setting was not right for the version of Java which was actually being run at the command line when you typed "java".
If you see this, you might try making sure that your JAVA_HOME environment variable points to the Java you installed and make sure that it appears first in your path.
javadoc says, Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.
guess java does not know how to read the image u specified.. use a .gif format.

Categories

Resources