Linux unsatisfiedlinkerror java library path

Error when using JNI on ubuntu: java.lang.UnsatisfiedLinkError: no . in java.library.path

I know there are similar questions on this topic but none of the answers could solve my problem: I have a java file:

class hjni < static < System.loadLibrary("hjni"); >private native void print(); public static void main(String[] args) < new hjni().print(); >> 
javac hjni.java javah -jni hjni 
 #include #include #include "hjni.h" JNIEXPORT void JNICALL Java_hjni_print(JNIEnv *env, jobject obj)
g++ -fPIC -shared -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/lib/jvm/java-7-openjdk-amd64/include/linux hjni.cpp -o hjni.so 
java -Djava.library.path=. hjni 
Exception in thread "main" java.lang.UnsatisfiedLinkError: no hjni in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1889) at java.lang.Runtime.loadLibrary0(Runtime.java:849) at java.lang.System.loadLibrary(System.java:1088) at hjni.(hjni.java:4) 
java.library.path = /usr/java/packages/lib/amd64 /usr/lib/x86_64-linux-gnu/jni /lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu /usr/lib/jni /lib /usr/lib 
cd /usr/java/packages/lib/amd64 bash: cd: /usr/java/packages/lib/amd64: No such file or directory 
/usr/lib/jvm/java-7-openjdk-amd64/lib/amd64/jli 
export LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/lib/amd64/jli 

the path is successfully added as long as the terminal is open. If I close the terminal and reopen it, then the added path is no longer there. I also copied hjni.so to the jli folder but I got the same linking error.

Источник

java.lang.UnsatisfiedLinkError no *****.dll in java.library.path

In order for System.loadLibrary() to work, the library (on Windows, a DLL) must be in a directory somewhere on your PATH or on a path listed in the java.library.path system property (so you can launch Java like java -Djava.library.path=/path/to/dir ).

Additionally, for loadLibrary() , you specify the base name of the library, without the .dll at the end. So, for /path/to/something.dll , you would just use System.loadLibrary(«something») .

You also need to look at the exact UnsatisfiedLinkError that you are getting. If it says something like:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no foo in java.library.path 

then it can’t find the foo library (foo.dll) in your PATH or java.library.path . If it says something like:

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.example.program.ClassName.foo()V 

then something is wrong with the library itself in the sense that Java is not able to map a native Java function in your application to its actual native counterpart.

To start with, I would put some logging around your System.loadLibrary() call to see if that executes properly. If it throws an exception or is not in a code path that is actually executed, then you will always get the latter type of UnsatisfiedLinkError explained above.

As a sidenote, most people put their loadLibrary() calls into a static initializer block in the class with the native methods, to ensure that it is always executed exactly once:

Читайте также:  What is linux kernel in android

By putting all dlls in System32 and using System.loadLibrary(«something») worked. I was doing System.loadLibrary(«something.dll») earlier. Why cant it load all dlls from WEB-INF? I guess it loads all jars by default. What can I do to load these dlls from WEB-INF directly instead of System32/specify them in java.library.path

+1 for the ‘use «bla» instead of «bla.dll» for loadLibrary() remark— very useful when you have no clue what you are doing wrong.

Thanks. It worked for me when I updated «PATH» for windows so that it contains folder having *.so file.

I could have sworn that OSX required blah.jnilib and Linux libblah.so . Well, two hours later, after numerous tries, I came to the conclusion that OSX does also require lib prefix

Changing ‘java.library.path’ variable at runtime is not enough because it is read only once by JVM. You have to reset it like:

System.setProperty("java.library.path", path); //set sys_paths to null final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths"); sysPathsField.setAccessible(true); sysPathsField.set(null, null); 

I get WARNING: An illegal reflective access operation has occurred and Exception in thread «main» java.lang.ExceptionInInitializerError and also Caused by: java.lang.NullPointerException when I do this.

The original answer by Adam Batkin will lead you to a solution, but if you redeploy your webapp (without restarting your web container), you should run into the following error:

java.lang.UnsatisfiedLinkError: Native Library "foo" already loaded in another classloader at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1715) at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1646) at java.lang.Runtime.load0(Runtime.java:787) at java.lang.System.load(System.java:1022) 

This happens because the ClassLoader that originally loaded your DLL still references this DLL. However, your webapp is now running with a new ClassLoader, and because the same JVM is running and a JVM won’t allow 2 references to the same DLL, you can’t reload it. Thus, your webapp can’t access the existing DLL and can’t load a new one. So. you’re stuck.

Tomcat’s ClassLoader documentation outlines why your reloaded webapp runs in a new isolated ClassLoader and how you can work around this limitation (at a very high level).

The solution is to extend Adam Batkin’s solution a little:

 package awesome; public class Foo < static < System.loadLibrary('foo'); >// required to work with JDK 6 and JDK 7 public static void main(String[] args) < >> 

Then placing a jar containing JUST this compiled class into the TOMCAT_HOME/lib folder.

Now, within your webapp, you just have to force Tomcat to reference this class, which can be done as simply as this:

Now your DLL should be loaded in the common classloader, and can be referenced from your webapp even after being redeployed.

Читайте также:  Directx 11 for linux

A working reference copy can be found on google code, static-dll-bootstrapper .

Источник

What is the cause of an UnsatisfiedLinkError?

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.

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

i would add that path should not end with slash/backslash — just with the name of the directory (oh god I was making this mistake)

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.

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.

Источник

java.lang.UnsatisfiedLinkError in Linux

I’ve managed to get into a linux machine to try the HotKey library suggested in this answer. I’ve compiled the sample code and now I run the program and I’ve got the following message:

[oracle@machine jxgrabkey-0.2.1_i386]$ java -classpath lib/JXGrabKey.jar:Example JXGrabKeyTest Exception in thread "main" **java.lang.UnsatisfiedLinkError:** /home/oracle/javasample/jxgrabkey-0.2.1_i386/lib/libJXGrabKey.so: /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by /home/oracle/javasample/jxgrabkey-0.2.1_i386/lib/libJXGrabKey.so) at java.lang.Runtime._load(libgcj.so.7rh) at java.lang.Runtime.load(libgcj.so.7rh) at java.lang.System.load(libgcj.so.7rh) at JXGrabKeyTest.main(JXGrabKeyTest.java:17)

I know how to handle this in Windows ( just by adding the DLL to the PATH env var ) but I’m not that sure about linux. I’ve read something about LD_LIBRARY_PATH and some other env vars but I can make it work. Any advice? EDIT After the support from mmyers to indentify the problem and reading this thread and this other. I can tell: My system is: Linux 2.6.18-53.el5 My GCC version(s) is ( are) : gcc-c++-4.1.2-14.el5 gcc-gfortran-4.1.2-14.el5 libgcc-4.1.2-14.el5 gcc-4.1.2-14.el5 The problems is I require gcc 4.2.0 Aaand apparently there is no gcc 4.2.0 for my system. I guess I would have to wait for it to come or the author recompile it in a previous version. mmyers, thanks a lot for your help.

Читайте также:  Acronis true image аналог linux

Источник

UnsatisfiedLinkError when using JNI?

I want to call a C program from Java program using JNI in linux ubuntu. I am new to this and I have tried the sample program given in http://www.ibm.com/developerworks/java/tutorials/j-jni/section2.html . I have already created the .java, .h , .c and .so files. But when i tried to run the program I am getting the following error.

Exception in thread "main" java.lang.UnsatisfiedLinkError: no Sample1 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1028) at Sample1.main(Sample1.java:13)

It is missing something called «Sample1». Ensure the shared object is built and in the indicated path.

@poonam Hoshi: Please write the steps used in creating the sharelibrary and command line/LD_LIBRARY_PATH used to pass the library to jvm..

2 Answers 2

This exception is indicating that the .so is not available to the JVM.

Adding the directory where the .so exists to the LD_LIBRARY_PATH will resolve this. If the .so depends on other .so libraries the directories where these .so exist will also need added to LD_LIBRARY_PATH .

I tried setting the library path but it is still not working. My ubuntu version is Ubuntu 10.04.4 LTS. I just learned from some other site that in this version of ubuntu LD_LIBRARY_PATH doesnot work anymore.Please help resolving this. and Thanks for the help.

I’ve just tried to get the same sample to work on my CentOS and got the same error as you. As already answered, JVM failed to find the so file needed. I succeeded to get it to work by following the steps below using gcc:

$ javac Sample1.java $ javah Sample1 $ # Include paths must also be specified using -I option in the following gcc command line! $ gcc -shared -I. snip. Sample1.c -o libSample1.so $ # Library path for libSample1.so must also be specified! $ java -Djava.library.path=. path/to/libSample1.so. Sample1 

If you omit the «lib» prefix of the shared library, JVM fails to find it for some reason. I don’t know why. I am not familiar with the naming convention of shared libraries in Linux.

I hope this post could help.

Источник

Оцените статью
Adblock
detector