Reputation: 368
I have a library called HelloWorld.so and a program HelloWorld.java with this content:
class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}
Now when I try to run HelloWorld.java I get this error:
$ /usr/java1.4/bin/java HelloWorld Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloWorld in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1491) at java.lang.Runtime.loadLibrary0(Runtime.java:788) at java.lang.System.loadLibrary(System.java:834) at HelloWorld.<clinit>(HelloWorld.java:7)
Any tips?
Upvotes: 9
Views: 25771
Reputation: 6564
I had this problem and fixed it by renaming my library to libHelloWorld.so
and following Michael Myers's suggestion. I'm on Arch Linux 64-bit.
HelloWorld.c
:
#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"
/* shamelessly stolen from the book 'The Java Native Interface: Programmer's
Guide and Specification' */
JNIEXPORT void JNICALL
Java_HelloWorld_print (JNIEnv *env, jobject obj) {
printf("Hello World!\n");
}
HelloWorld.java
:
class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}
Building and testing:
$ javac HelloWorld.java
$ javah -classpath . HelloWorld
$ gcc -shared -fPIC -I $JAVA_HOME/include -I $JAVA_HOME/include/linux HelloWorld.c -o libHelloWorld.so
$ java -classpath . -Djava.library.path=. HelloWorld
Hello World!
tl;dr: put lib
at the beginning of the library's filename
Upvotes: 19
Reputation: 181
I think some points are helpful when getting this error:
System.loadLibrary("HelloWorld");
libHelloWorld.so
libHelloWorld.so
HelloWorld.dll
libHelloWorld.jnilib
-Djava.library.path=PATH
. PATH
to place where you put your jni libraryHere is my reference: https://blogs.oracle.com/moonocean/entry/a_simple_example_of_jni
Upvotes: 14
Reputation: 368
@mmyers Thank you for responding. We found out that all we had to do was change System.loadLibrary to System.load and pass the full path + filename as argument, worked like a charm.
Even before doing so, we tried using the "-D" parameter and setting LD_LIBRARY_PATH but we weren't successful.
Go figure! :)
Thanks again, Karen
Upvotes: 2
Reputation: 192055
Where is HelloWorld.so located? You probably need to specify its parent directory using the command-line parameter "-Djava.library.path"
.
For example, if it's in "/path/libs/HelloWorld.so"
, add -Djava.library.path=/path/libs
as an option when invoking java
. For instance, it's "-Djava.library.path=lib"
on one of my projects.
Edit: Dan Dyer points out that the environment variable LD_LIBRARY_PATH
also can be used for this.
Upvotes: 7