What is the cause of an UnsatisfiedLinkError? - java

When i am trying to run my program it is giving the following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jacob-1.14.3-x86 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1030)
at com.jacob.com.LibraryLoader.loadJacobLibrary(LibraryLoader.java:184)
at com.jacob.com.JacobObject.<clinit>(JacobObject.java:108)
at javaSMSTest.main(javaSMSTest.java:18)
please help

From the Javadoc:
Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.
It is an error related to JNI. loadJacobLibrary is trying to load the native library called jacob-1.14.3-x86 and it is not found on the path defined by java.library.path. This path should be defined as a system property when you start the JVM. e.g.
-Djava.library.path=<dir where jacob library is>
On Windows, the actual native library file will be called jacob-1.14.3-x86.dll while on Linux it would be called libjacob-1.14.3-x86.so

You need the jacob-1.14.3-x86 library on your java library path.
On windows, this would be jacob-1.14.3-x86.dll.
This is a binary file which is used by java to run native methods. It's probably required by some library (jar) you're using.
In here you can see not only a jar, but also the binary required by the jar. Pick the one for your platform.

To quote http://www.velocityreviews.com/forums/t143642-jni-unsatisfied-link-error-but-the-method-name-is-correct.html:
There are two things that cause UnsatisfiedLinkError. One is when
System.loadLibrary() fails to load the library, the other is when the
JVM fails to find a specific method in the library. The text of the
error message itself will indicate which is the case...
The error which you describe clearly cannot find the library at all. As the others have said, include it in your Java library path.
The other error—when the library can be found but the method within the library is not found—looks as follows:
java.lang.UnsatisfiedLinkError: myObject.method([Ljava/lang/Object;)V
In this case you either have the wrong method name, or will have to go back and add the method and recompile the code...

Related

System.loadLibrary loads something else

In my java code, I call System.loadLibrary(A); to load the A.so library. However, my app crashes at run time with this error:
java.lang.UnsatisfiedLinkError: dlopen failed: library "B.so" not found
Admittedly, the B.so library is a dependency of A.so library. But I did not explicitly call System.loadLibrary() on B.so anywhere. I was under the impression that B.so will be covered by A.so. Why is the Java runtime trying to load B.so?
Thanks!

Unable to load library NLPIR.dll in JNA

I try to use a word-segmentation software, I create a new java project named JNA, and import the required files into JNA dir, then I try to run it without any code modification, and MyEclipse warns me that:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'E://java//JNI//JnaTest_NLPIR//NLPIR': Native library (win32-x86-64/E://java//JNI//JnaTest_NLPIR//NLPIR.dll) not found in resource path ([file:/D:/javasoft/MyEclipse_workspace/JNA/bin/, file:/D:/javasoft/MyEclipse_workspace/JNA/lib/jna-4.0.0.jar])
I tried to:
create the same dirs and move NLPIR.dll to E:/java/JNI/JnaTest_NLPIR/, it didn't work.
move NLPIR.dll directly to JNA/bin or JNA/lib, but it didn't work, too.
I think that it is caused by the position of my NLPIR.dll. But I could not figure out how to fix it.
I would be grateful for any help or suggestions.
Ensure NLPIR.dll (and all its dependent libraries) is on %PATH%, or use -Djna.library.path=... to indicate the folder it's in.

Calling C++ from Java, but Java loads the wrong Glibc version

I am trying to call a C++ library from java side and have written necessary jni code. However, when my java code tries to load my C++ library via System.loadLibrary, it complains the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: libmylib.so: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by libmylib.so)
I later found that it is because I have two different GLIBC versions on my system, and the default one is the older one, while libmylib.so is required to built from the newer GLIBC. As a result, java links to the wrong GLIBC.
I tried the following things, but it does not work :(.
The first one is to try to load correct library manually via System.load in my java code. Specifically, I added the following codes before it loads my library:
static {
System.load("/usr/local/gcc-4.8.1-glibc-2.17/lib/libc.so.6");
System.load("/usr/local/gcc-4.8.1-glibc-2.17/lib/libstdc++.so.6.0.18");
System.loadLibrary(mylib);
}
The libc.so.6 is also added because libstdc++ depends on it (otherwise java will load the wrong libc and complains another error). However, this time java complains the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
/usr/local/gcc-4.8.1-glibc2.17/lib/libc-2.17.so:
__vdso_time: invalid mode for dlopen(): Invalid argument
And this error is caused by mixing two versions of GLIBC as described in this question.
The solution to that question is for building C++ program with -Wl,--dynamic-linker set properly (also described here). However, I don't know how to do that in Java.
I tried to set LD_LIBRARY_PATH to the newer version, but the situation is just the same :(.
Can I know how to make java link to the correct library?
(PS: a solution without make install newer version of glibc is preferred, since many other applications in my machine rely on the current default glibc)
I have been searching and trying solutions to my problem for days, but none of them succeed :(
Stackoverflow, you're my only hope :~.
System.load("/usr/local/gcc-4.8.1-glibc-2.17/lib/libc.so.6");
As I explained here, this can not possibly work.
By the time you reach above statement, the system libc.so.6 has already been loaded (at process startup), and you absolutely can not have two versions of libc.so.6 loaded into the same process.
Try using LD_PRELOAD=new_c_library.so java -javastuff

Java byte code instrumentation with an external library

I am using ASM with java agents. I have the following problem.
Whenever I see a "PUTFIELD" instruction within a method call, I want call a method from my agent library.
if (opcode == PUTFIELD) {
super.visitMethodInsn(Opcodes.INVOKESTATIC, "instrumenter/Util", "debug", "()V");
Util is a class defined by me with a static debug method. It stays in my agent.jar
java -javagent:agent.jar -jar test.Test works as I expected.
However, when I test this agent with some other jar files I got following error.
Exception in thread "main" java.lang.NoClassDefFoundError: instrumenter/Util
I suspect this occurs due to concurrency. Since the programs which create this error are mostly multi-threaded.
you could try to use -bootclasspath/p instead of -jar, probably, something is loaded too early for your util-class or some classloader-issue (e.g. a different (custom) classloader which cannot access your jar). if you put your jar into the bootclasspath, at least this source of defect is eliminated

Mac + jni + java

A little background:
I have a java application that needs to talk to a third party hardware on mac. They have given me the sdk but it is not in Java. So I am trying to make jnilib that will act as a bridge between my java application and the SDK.
The issue:
I have made a small sample jnilib that talks to the SDK but when I try to use it in my java program I get the following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: /Users/john.doe/Desktop/eclipse/workspace/Lesson13_Jni_Smart7/bin/libSmartTest7.jnilib: Library not loaded: build/Release/SMARTResponseSDK.framework/Versions/A/SMARTResponseSDK Referenced from: /Users/john.doe/Desktop/eclipse/workspace/Lesson13_Jni_Smart7/bin/libSmartTest7.jnilib
Reason: image not found
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1827)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1742)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1045)
at com.learning.lesson13.JniSmart7.<clinit>(JniSmart7.java:6)
From the error it looks like my libSmartTest7.jnilib is looking for the library SMARTResponseSDK.
What I have tried
I know where the library SMARTResponseSDK is on my Mac. I tried copying it over to my working folder in eclipse but I still get the error. I have tried using the -DJava.library.path but I still get the error.
Any ideas on what the best possible approach would be.
Once you are inside JNI code, it no longer matters what java.library.path points at.
Once you are inside JNI code, all you can do is to make sure library is visible to your code via LD_LIBRARY_PATH/DYLD_LIBRARY_PATH, or you can dynamically load your library file from any location you like.
So, for you, I suggest to take a look here:
dynamic loading of library in JNI - http://jnicookbook.owsiak.org/recipe-No-018/
Calling code from shared library (adapter pattern) - http://jnicookbook.owsiak.org/recipe-No-023/
You can also benefit from compilation flags while building your JNI library and use rpath.

Categories

Resources