Errors happen when I run the jar file in terminal - java

I made libcc.so from my test.cpp in ubuntu, and the content of test.cpp is linked with ffmpeg and opencv libraries. After making the libcc.so, it's loaded in JNItest2.java.
JNItest2.java:
public class JNItest2 {
public native void helloworld();
static{
String libPath= "/home/sun/workspace/JNItest2/src";
System.setProperty("java.library.path",libPath);
String Path = System.getProperty("java.library.path");
System.out.println("java.library.path=" + Path);
System.loadLibrary("cc");
}
public static void main(String[] args){
new JNItest2().helloworld();
}
}
However, JNItest2.java can run on Eclipse, the exported jar file can't run in terminal.
Error Message in terminal:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no test in
java.library.path at
java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) at
java.lang.Runtime.loadLibrary0(Runtime.java:870) at
java.lang.System.loadLibrary(System.java:1122) at JNItest2.<clinit>
(JNItest2.java:23)
Does anyone help me to solve this problem?
Thanks, Hsiu

Take a look here for a sample related to JNI development:
http://jnicookbook.owsiak.org/recipe-No-001/
This is a very simple app where you can take a look at all required elements for running JNI based codes.
In your case:
make sure to put so file in LD_LIBRARY_PATH
if you want to use ffmpeg stuff in your code make sure to check out these two samples as well:
http://jnicookbook.owsiak.org/recipe-No-018/
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo023

Related

"java.lang.UnsatisfiedLinkError: 'long org.RDKit.RDKFuncsJNI.RWMol_MolFromSmiles__SWIG_3(java.lang.String)'"

I want to use RDkit in my java web project. It happens like this "I want to use RDkit in my java web project. It happens like this".
It is so weird.I wirte a sample java application like
public static void main(String[] args) {
System.loadLibrary("GraphMolWrap");//I have put the .dll file in the path
String smiles = "CN1CCN2C(C1)C1=C(CC3=C2C=CC=C3)C=CC=C1";//my pom.xml import the jar
RWMol m1 = RWMol.MolFromSmiles(smiles);
}
It runs without any bug!
But When I put its code in my springboot project.It happens like title.
I know where's is the bug.It is about the maven package for springboot.If run with the command,it will run formally.

Getting an UnsatisfiedLinkError (undefined symbol) in Java while loading JNI dependencies even after successfully loading the required object file

I'm using Google OR-tools library (v6.4) for a project (though my question is not specific to this library). This consists of one jar, which has a few native dependencies (a bunch of ".so"/".dylib" object files, depending on the OS). This build for my project is being made on Ubuntu 14.04
The problem I'm facing: On trying to load a specific object file at runtime (using System.load()), I'm getting an UnsatisfiedLinkError with the message as "undefined symbol" (I've added the stacktrace below). However, I am loading the object file defining this symbol just before this, so I'm not sure why this error is being thrown.
I'm loading the dependencies in the following way: The object files are being packed into the jar created by Maven during build, and are being extracted and loaded (using System.load()) at runtime. The method for that is as follows:
public class EnvironmentUtils {
public static void loadResourceFromJar(String prefix, String suffix) {
String tempFilesDirectory = System.getProperty("java.io.tmpdir");
File tempFile = null;
try {
tempFile = new File(tempFilesDirectory + "/" + prefix + suffix);
tempFile.deleteOnExit();
try (final InputStream inputStream = EnvironmentUtils.class.getClassLoader().
getResourceAsStream(prefix+suffix)) {
if (inputStream == null) {
throw new RuntimeException(prefix + suffix + " was not found inside JAR.");
} else {
Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
System.load(tempFile.getAbsolutePath());
} catch (Exception e) {
//Log top 10 lines of stack trace
}
}
}
This method is being called inside a static block for all dependencies:
public class DummyClass {
static {
String sharedLibraryExtension = EnvironmentUtils.getSharedLibraryExtension(); //.so for linux, .dylib for Mac
String jniLibraryExtension = EnvironmentUtils.getJniLibraryExtension(); //.so for linux, .jnilib for Mac
EnvironmentUtils.loadResourceFromJar("libfap", sharedLibraryExtension);
EnvironmentUtils.loadResourceFromJar("libcvrptw_lib", sharedLibraryExtension);
EnvironmentUtils.loadResourceFromJar("libortools", sharedLibraryExtension);
EnvironmentUtils.loadResourceFromJar("libdimacs", sharedLibraryExtension);
EnvironmentUtils.loadResourceFromJar("libjniortools", jniLibraryExtension);
}
}
On running System.load() for libdimacs.so, an UnsatisfiedLinkError is thrown. Stacktrace:
java.lang.UnsatisfiedLinkError: /tmp/libdimacs.so: /tmp/libdimacs.so: undefined symbol: _ZN6google14FlagRegistererC1IbEEPKcS3_S3_PT_S5_
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
at java.lang.Runtime.load0(Runtime.java:809)
at java.lang.System.load(System.java:1086)
at com.(PROJECT_NAME).utils.EnvironmentUtils.loadResourceFromJar(EnvironmentUtils.java:78)
at com.(PROJECT_NAME).DummyClass.<clinit>(DummyClass.java:28)
However, this symbol "_ZN6google14FlagRegistererC1IbEEPKcS3_S3_PT_S5_" is present in libortools.so, which is being loaded before libdimacs. I verified this by running the following command:
objdump -t (LIBRARY_PATH)/libortools.so | grep _ZN6google14FlagRegistererC1IbEEPKcS3_S3_PT_S5_
This gave me the following output:
0000000000ce12cc gw F .text 00000091 _ZN6google14FlagRegistererC1IbEEPKcS3_S3_PT_S5_
So it would seem that the symbol should have been defined at the time of the System.load() call, unless there was some issue in loading the containing object file. To check if the object file had been loaded correctly, I used the approach detailed in this solution. Apart from the class detailed in that answer, I added the following lines after System.load() call in EnvironmentUtils.loadResourceFromJar() to print the most recently loaded library name:
public class EnvironmentUtils {
public static void loadResourceFromJar(String prefix, String suffix) {
...
System.load(tempFile.getAbsolutePath());
final String[] libraries = ClassScope.getLoadedLibraries(ClassLoader.getSystemClassLoader());
System.out.println(libraries[libraries.length - 1]);
}
}
The output (till just before the UnsatisfiedLinkError) is as follows:
/tmp/libfap.so
/tmp/libcvrptw_lib.so
/tmp/libortools.so
So libortools.so seems to be loading correctly, which means the symbol should be loaded in memory. The exact same code is working perfectly with the corresponding Mac (".dylib") dependencies (Built on MacOS Sierra 10.12.5). Would appreciate any advice on resolving this. Thank you.
I'm apologize that the java artifact may be broken currently...
you can use c++filt to demangle the symbol ;)
c++filt _ZN6google14FlagRegistererC1IbEEPKcS3_S3_PT_S5_
google::FlagRegisterer::FlagRegisterer<bool>(char const*, char const*, char const*, bool*, bool*)
In fact gflag has recently change its namespace from google:: to gflags:: and glog or protobobuf? try to find the correct one and I guess it failed...
note: Still not completely sure whose is the bad guy who use the google:: namespace since libortools merge all its static dependencies but I guess now you understand the bug...
note2: I have a patch in mizux/shared branch https://github.com/google/or-tools/commit/805bc0600f4b5645114da704a0eb04a0b1058e28#diff-e8590fe6fb5044985c8bf8c9e73c0d88R114
warning: this branch is currently broken and not ready yet. I'm trying ,for unix, to move from static to dynamic dependencies, so I need to fix all rpath, transitives deps etc... and in the process I also had to fix this issue (that I didn't reproduced while using static dependencies)
If too long to finish (we should create a release 6.7.2 or 6.8 (i.e. new artifact) by the end of May 2018) which maybe only contains this fix and not my branch...

UnsatsifiedLinkError on JSSC Serial Interface (Processing)

I'm using Proclipsing (processing in Eclipse) but am getting an error when I try and open a port (printing the serial list works fine). I have a feeling some sort of native library is not connected but I'm baffled as to how to do so in Eclipse (and where it link to).
Here's my code:
import processing.core.PApplet;
import processing.serial.Serial;
public class visualization extends PApplet {
public Serial usb = null;
public void setup() {
println(Serial.list());
println(Serial.list()[5]);
usb = new Serial(this, Serial.list()[5], 115200);
}
public void draw() {
}
}
and the error it throws is:
Exception in thread "Animation Thread" java.lang.UnsatisfiedLinkError: jssc.SerialNativeInterface.openPort(Ljava/lang/String;Z)J
at jssc.SerialNativeInterface.openPort(Native Method)
at jssc.SerialPort.openPort(SerialPort.java:158)
at processing.serial.Serial.<init>(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at bioauthvisualization3.BioauthVisualization3.setup(BioauthVisualization3.java:15)
at processing.core.PApplet.handleDraw(PApplet.java:2361)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
at processing.core.PApplet.run(PApplet.java:2256)
at java.lang.Thread.run(Thread.java:745)
I ran into the same issue while setting up IntelliJ IDEA for development with Processing. But I imagine you can apply the same method in Proclipsing. The solution that worked for me was threefold.
I created a global library that contained the Processing and Serial libraries. In OS X the required jar files can be found in the directory /Applications/Processing.app/Contents/Java/ and /Applications/Processing.app/Contents/Java/modes/libraries/serial/library/. I added the following libraries: core.jar, serial.jarand jssc.jar.
Next I had to add the global library to my module dependencies.
The last step was to add the path to the native libraries to the VM options for the appletviewer: -Djava.library.path="/Applications/Processing.app/Contents/Java/modes/java/libraries/serial/library/macosx"
My guess is that Proclipsing does the first two steps for you but you have to add the native libraries to the vm options manually since these depend on the system you're working on. Hope that helps.
I derived the solution for step 3 from this forum entry.

UnsatisfiedLinkError, despite setting Djava.library.path variable

I am trying to integrate IBM's CPLEX library with my java application. For now, i'm just trying to create an IloCplex object. I added Cplex.jar, and it compiles fine, but when I run this:
public class cplexTest{
public static void main(String[] args){
try{
IloCplex cplex = new IloCplex();
}catch (Exception e){
e.printStackTrace();
}
}
Cplex prints this message before throwing an exception:
java.lang.UnsatisfiedLinkError: no cplex124 in java.library.path
java.library.path must point to the directory containing the CPLEX shared library
try invoking java with java -Djava.library.path=...
I pass this argument to the JVM: -Djava.library.path="C:\Program Files\IBM\ILOG\CPLEX_Studio124\cplex\bin\x64_win64. This is the location of cplex124.dll. Every tutorial i've seen gives the same steps, and i feel like i followed them pretty well.
What am I doing wrong??
What I've found in the interwebs is that one possible cause for this problem is the dll being in 32 or 64 bits and your java being in the opposite architecture.
Try verifying that your java and cplex.dll match.

javaCV in NETBEANS on windowsXP platform

Dear friends...
Here i am trying to use openCV in java using NETBEANS(windows XP).., For that i did the following ..
[B]1-Install OpenCV-2.2.0-win32-vs2010.exe in C:\openCV
2-Then i put the javaCV and JNA in the library (System path.)
3-The i use thae following code in the netbeans[/B]
[CODE]
package samplejavacv;
import java.io.;
import static com.googlecode.javacv.jna.cxcore.;
import static com.googlecode.javacv.jna.cv.;
import static com.googlecode.javacv.jna.highgui.;
public class sample {
public static void main(String[] args)throws Exception {
try {
IplImage Iimg=cvLoadImage("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.jpg");
}
catch(Exception f) {
System.out.print(f.getMessage());
}
}
}
[/CODE]
4- But i got the following exception
[CODE]
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'cxcore': The specified module could not be found.
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:163)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:236)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:199)
at com.googlecode.javacv.jna.Loader.load(Loader.java:44)
at com.googlecode.javacv.jna.cxcore.<clinit>(cxcore.java:113)
at com.googlecode.javacv.jna.highgui.<clinit>(highgui.java:73)
at samplejavacv.sample.main(sample.java:49)
Java Result: 1
[/CODE]
5-Then what should i do ....please help
6- Remember some site mention that it need to build the openCV , if it need how can i do that please help...
Anyway thanks in advance .....
Happynew year
You will need compiled *.DLL (for 32 or AMD 64 windows platform or *.SO for e.g. linux) files like:
-cv.dll
-cvaux.dll
-cvauxd.dll
-cvd.dll
-cxcore.dll
-cxcored.dll
-cxts.dll
-cxtsd.dll
-highgui.dll
-highguid.dll
-md.dll
-ml.dll
than run your JAVA VM with -Djna.library.path=c:\path\to\your\dlls\directory. E.g.:
java -Djna.library.path=c:\path\to\your\dlls\directory -cp=. Test

Categories