Running Java OpenCV on Ubuntu from command line - java

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

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.

OpenCV 2.4.13.4 dll link error

I am running the following code snippet for OpenCV:
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());
}
I get this error:
Welcome to OpenCV 2.4.13.4
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_eye(III)J
at org.opencv.core.Mat.n_eye(Native Method)
at org.opencv.core.Mat.eye(Mat.java:1468)
at HelloCV.main(HelloCV.java:29)
I have tried the following:
Putting the dll full path name on System.loadLibrary
Passing the dll path to JVM
Putting the dll file into the windows system32 folder
Putting the dll path name into eclipse opencv.jar config
It seems there was an issue with the following eclipse version:
Eclipse IDE for Java Developers
Version: Oxygen.1a Release (4.7.1a)
Build id: 20171005-1200
OS: Windows 10, v.10.0, x86_64 / win32
The below eclipse version works:
Eclipse SDK
Version: Neon (4.6)
Build id: I20160606-1100

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"));

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.

Categories

Resources