Can't load OpenCV on Linux - undefined symbol error - java

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.

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.

UnsatisfiedLinkError Trying to run OpenCV java code cross compiled

I'm beating my head against a wall here. There are a lot of very similar questions but none exactly on point. I'm sure the answer is staring me in the face.
I'm conducting my first OpenCV cross compile test. I set up OpenCV 3.1.0 in Eclipse on Windows 10. Code there runs fine. From there I created an executable .jar and FTP'd it to a raspberry pi 3 running Jessie.
I installed OpenCV 3.1.0 on the Pi including java support (on the third try). But I continue to get the same error.
Code:
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class Hello
{
public static void main( String[] args )
{
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
System.out.println( "mat = " + mat.dump() );
}
}
Error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java310 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at Hello.main(Hello.java:9)
I've found opencv-java310.jar in /usr/local/share/OpenCV/java
And I have tried at the command line both with and without the -D parameter:
java -jar HelloEx.jar -D java.library.path=/usr/local/share/OpenCV/java
Thanks in advance
I'm not sure if you correctly set java.library.path, as in your starting post it is incorrect, please try use it as follows: java -Djava.library.path=/usr/local/share/OpenCV/java -jar HelloEx.jar and you also may check if it was set correctly in your java code by adding the following code (just to debug): System.out.println(System.getProperty("java.library.path"));

Running Java OpenCV on Ubuntu from command line

I'm trying to compile and run a simple example of Java code using OpenCV library, on Ubuntu 14.04 64bits, found in OpenCV's documentation:
Java OpenCV Documentation
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;
class SimpleSample
{
static
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args)
{
System.out.println("Welcome to OpenCV " + Core.VERSION);
Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
System.out.println("OpenCV Mat: " + m);
Mat mr1 = m.row(1);
mr1.setTo(new Scalar(1));
Mat mc5 = m.col(5);
mc5.setTo(new Scalar(5));
System.out.println("OpenCV Mat data:\n" + m.dump());
}
}
Since it is just a simple test, I really don't want to use any sophisticated building system. So I'm compiling with the following command:
javac -cp "/usr/share/OpenCV/java/opencv-248.jar" SimpleSample.java
However I'm unable to run the application, because when I call:
java SimpleSample
I face the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Core
at SimpleSample.<clinit>(SimpleSample.java:10)
Caused by: java.lang.ClassNotFoundException: org.opencv.core.Core
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Can someone help me running this application?
The opencv-248.jar is not on the CLASSPATH. You can run it as follows:
java -cp /usr/share/OpenCV/java/opencv-248.jar:. SimpleSample
Compile from the source and copy libopencv_java248.so into $JAVA_HOME/jre/lib/amb64/ directory. This platform specific file is not included in the jar file. Running the sample program without this file installed would get "java.lang.UnsatisfiedLinkError: no opencv_java248 in java.library.path" error. Also note that Apache Ant need to be installed for this platform specific file to be generated during compilation.
I managed to solve the problem.
In Ubuntu 14.04 64bit you can run the code from command line with the following command:
LD_LIBRARY_PATH=/usr/lib/jni/ java -cp /usr/share/OpenCV/java/opencv-248.jar:. SimpleSample
The OpenCV native binary library path (in my case libopencv_java343.dylib) can be defined with -D:
java -cp /opt/local/share/OpenCV/java/opencv-343.jar:. -Djava.library.path=/opt/local/share/OpenCV/java/ SimpleCV

Eclipse can't find my installed Ruby gems (JRuby)

So, I am trying to use my ruby scripts inside my java code with the help of JRuby. But there is a problem, eclipse is not being able to use the gems I have installed. For testing purposes, I've been trying to run a piece of code that requires the "nokogiri" ruby gem, which I have installed. When I try to run the java code, here's what I get:
LoadError: no such file to load -- nokogiri
require at org/jruby/RubyKernel.java:939
<top> at /home/amng/workspace/scripts/xx/x/getMSPatches:4
Exception in thread "main" org.jruby.embed.EvalFailedException: (LoadError) no such file to load -- nokogiri
at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:131)
at org.jruby.embed.ScriptingContainer.runUnit(ScriptingContainer.java:1307)
at org.jruby.embed.ScriptingContainer.runScriptlet(ScriptingContainer.java:1352)
at jobs.Teste.run(Teste.java:17)
at jobs.Teste.main(Teste.java:11)
Caused by: org.jruby.exceptions.RaiseException: (LoadError) no such file to load -- nokogiri
at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:939)
at RUBY.(/home/amng/workspace/scripts/myPrecious/MSPatches/getMSPatches:4)
I downloaded the jruby.jar from the website and added to the build path of the project. I also pointed the eclipse to the JRuby binary using the Dynamic Languages Toolkit (DLTK) plugin. What do I have to do to make sure that eclipse can use the gems I have installed?
Edit: My java code:
import org.jruby.embed.ScriptingContainer;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.PathType;
public class Main {
public static void main(String[] args) {
ScriptingContainer ruby = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
Object result = ruby.runScriptlet(PathType.ABSOLUTE, "/home/amng/workspace/scripts/myPrecious/MSPatches/getMSPatches");
System.out.println(result);
}
}
The first two lines of my (working) ruby code (getMSPatches) are:
# encoding: UTF-8
require 'nokogiri'
Try setting the jruby home directory
ScriptingContainer ruby = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
ruby.setHomeDirectory("/jruby-9.0.5.0");
This solved the issue for me.

Error when executing a jcuda program in eclipse

I'm new with JCuda. I try to write a simple example in Eclipse on Linux. I have this error, but I haven't understand what it means. Here the code:
import jcuda.Pointer;
import jcuda.runtime.JCuda;
public class cudaTest {
public static
void main(String[] args) {
Pointer pointer = new Pointer();
JCuda.cudaMalloc(pointer, 4);
System.out.println("Pointer: " + pointer);
JCuda.cudaFree(pointer);
}
}
I add the Jcuda.jar from the Java Build Path, and edit the Native library location by selecting the extracted JCuda file.
The error is:
Error while loading native library "JCudaRuntime-linux-x86_64" with base name "JCudaRuntime"
Operating system name: Linux
Architecture : amd64
Architecture bit size: 64
Stack trace from the attempt to load the library as a resource:
java.lang.NullPointerException: No resource found with name '/lib/libJCudaRuntime-linux-x86_64.so'
at jcuda.LibUtils.loadLibraryResource(LibUtils.java:151)
at jcuda.LibUtils.loadLibrary(LibUtils.java:83)
at jcuda.runtime.JCuda.initialize(JCuda.java:303)
at jcuda.runtime.JCuda.<clinit>(JCuda.java:290)
at cudaTest.main(cudaTest.java:8)
Stack trace from the attempt to load the library as a file:
java.lang.UnsatisfiedLinkError: /home/Faith/JCuda-All-0.4.2-bin-linux-x86_64/libJCudaRuntime-linux-x86_64.so: libcudart.so.4: Ne peut ouvrir le fichier d'objet partagé: Aucun fichier ou dossier de ce type
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1880)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at jcuda.LibUtils.loadLibrary(LibUtils.java:94)
at jcuda.runtime.JCuda.initialize(JCuda.java:303)
at jcuda.runtime.JCuda.<clinit>(JCuda.java:290)
at cudaTest.main(cudaTest.java:8)
Exception in thread "main" java.lang.UnsatisfiedLinkError: Could not load the native library
at jcuda.LibUtils.loadLibrary(LibUtils.java:129)
at jcuda.runtime.JCuda.initialize(JCuda.java:303)
at jcuda.runtime.JCuda.<clinit>(JCuda.java:290)
at cudaTest.main(cudaTest.java:8)
Could tou help me please, I'm really need the solution. Thank you in advance.
Look for libJCudaRuntime-linux-x86_64.so file in your system, and make a symoblic link to /lib/libJCudaRuntime-linux-x86_64.so OR add the path of the directory where you find it to your ldconfig
Copy the .so files to lib directory:
$ sudo cp *.so /lib/
One of the possible idea - is to give a try to my project called Mavenized JCuda
https://github.com/MysterionRise/mavenized-jcuda
It's specifically created for the purpose of not fighting against classpath and other problems. It's really easy to use it, all you need to do - is to put needed version of JCuda in pom.xml, then run mvn clean package and then mvn exec:exec.
Full HowTo is available on Github page. Feel free to ask me question about that or raise question about the project

Categories

Resources