Problem connecting Java and Prolog with JPL - java

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.

Related

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.

Ensuring files are available to the JVM

I'm trying to install TensorFlow for Java on Windows 10 using this Article
. I followed the steps carefully but the windows commands didn't work with me so I decided to do it manually.
The first command is to make the .jar part of the classpath and I did it manually
but the second step was to ensure that the following two files are available to the JVM: the .jar file and the extracted JNI library
but I don't know how to do that manually
The code:
package securityapplication;
import org.tensorflow.TensorFlow;
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
public class SecurityApplication {
public static void main(String[] args) throws Exception {
try (Graph g = new Graph()) {
final String value = "Hello from " + TensorFlow.version();
// Construct the computation graph with a single operation, a constant
// named "MyConst" with a value "value".
try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
// The Java API doesn't yet include convenience functions for adding operations.
g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
}
// Execute the "MyConst" operation in a Session.
try (Session s = new Session(g);
Tensor output = s.runner().fetch("MyConst").run().get(0)) {
System.out.println(new String(output.bytesValue(), "UTF-8"));
}
}
}
}
could someone help? cuz my program that uses TensorFlow still have the following error
The text in the image is :
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: windows, architecture: x86. See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md for possible solutions (such as building the library from source). Additional information on attempts to find the native library can be obtained by adding org.tensorflow.NativeLibrary.DEBUG=1 to the system properties of the JVM.
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.TensorFlow.init(TensorFlow.java:36)
at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:40)
at org.tensorflow.Graph.<clinit>(Graph.java:194)
at securityapplication.SecurityApplication.main(SecurityApplication.java:15) Java Result: 1 BUILD SUCCESSFUL (total time: 4 seconds)
The result after running the first command in cmd:
The result after running the second command in Windows PowerShell:
Any suggestions?!
Thank you
The first command failure (javac) suggests that the javac command is not in your PATH environment variables. See for example, this StackOverflow question
For the second command failure, I believe the space after -D is what is causing you trouble as Holger suggested.
IDEs like Eclipse and others also provide a means to set the java.library.path property for the JVM (see this StackOverflow answer for example).
Background: TensorFlow for Java consists of a Java library (packaged in a .jar file) and a native library (.dll on Windows, distributed in a .zip file). You need to ensure that the .jar file is in the classpath and the directory containing the .dll is in included in the java.library.path of the JVM when executing a program.
Hope that helps.

System cannot Load Native Library mvm.dll

I am working on Calling matlab function in java code by using Java Matlab Engine API in Eclipse.
I have written very simple program but its giving me runtime error.
I have written this code:
import com.mathworks.engine.*;
import com.mathworks.*;
public class Matlab_main {
public static void main(String[] args) throws Exception
{
MatlabEngine eng = MatlabEngine.startMatlab();
eng.evalAsync("disp('hello world')");
eng.close();
} }
I have done the following steps:
I have added engine.jar file in its library build path .
I have also set its java.library.path = "C:\Program Files\MATLAB\R2013a \bin\win64" by going into its Native Library settings.
I have also added this entry C:\Program Files\MATLAB\R2013a\bin\win64 to PATH environment variables
But its giving me error that System cannot Load Native Library mvm.dll
This dll is present in this directory C:\Program Files\MATLAB\R2013a\bin\win64,but eclipse is not finding that.
I am totally stuck, Please kindly help me

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

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