Linux: UnsatisfiedLinkError: no hello in java.library.path

T

tony_lincoln

Dear Friends,

I am using JNI to call C++ codes from Java, under Linux. This calling
worked in my Win2000, but not in Linux. The source codes came from
official java website:

*********************************************************************
//HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();

static {
System.loadLibrary("hello");
}

public static void main(String[] args) {
new HelloWorld().displayHelloWorld();
}
}
**********************************************************************

I compiled it:
Javac HelloWorld.java
Javah -jni HelloWorld

then compile the following c++ codes:
**********************************************************************
//HelloWorldImp.c:
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>

JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");
return;
}
************************************************************************

gcc *.c -shared -o hello.so
Then "java HelloWorld",
The following error comes:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello in
java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1517)
at java.lang.Runtime.loadLibrary0(Runtime.java:788)
at java.lang.System.loadLibrary(System.java:834)
at HelloWorld.<clinit>(HelloWorld.java:5)

I tried to copy the hello.so into /usr/lib/java/jre/lib/i386, but it
did not work.
Why and how?
 
G

Gordon Beaton

I am using JNI to call C++ codes from Java, under Linux. This
calling worked in my Win2000, but not in Linux. The source codes
came from official java website:
[...]
System.loadLibrary("hello");
[...]

Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello in
java.library.path
I tried to copy the hello.so into /usr/lib/java/jre/lib/i386, but it
did not work.

Your call to System.loadLibrary() is correct, but the file needs to be
named libhello.so.

Put the file into one of the directories mentioned in

- /etc/ld.so.conf
- LD_LIBRARY_PATH
- java.library.path

....or put it somewhere else, but modify one of the above three things
so that it mentions the location of the file.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top