Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JNI code and UnsatisfiedLinkError

843829Aug 4 2005 — edited Oct 14 2005
Hi,

I am testing JNI code sample. JVM is not loading the native library. It says: Exception in thread "main" java.lang.UnsatisfiedLinkError: no helloWorld.so in java.library.path

These are the code details:

//HelloWorld.java

package jni.test;

import java.lang.reflect.*;

public class HelloWorld
{
public native void displayMessage();
static
{
try{
System.out.println(System.getProperty("java.library.path"));
System.loadLibrary("helloWorld.so");
}catch(Exception err){
err.printStackTrace();
}
}
public static void main(String args[]){
HelloWorld h = new HelloWorld();
h.displayMessage();
}
}


// jni_test_HelloWorld.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class jni_test_HelloWorld */

#ifndef Includedjni_test_HelloWorld
#define Includedjni_test_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: jni_test_HelloWorld
* Method: displayMessage
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_jni_test_HelloWorld_displayMessage
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

//HelloWorld.c

include <stdio.h>
#include <jni.h>
#include "jni_test_HelloWorld.h"

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

Generated so file with this command:


gcc -shared -O6 -fPIC -I/usr/java/jdk1.5.0_01//include -I/usr/java/jdk1.5.0_01//include/genunix -I/usr/java/jdk1.5.0_01//include/linux -Dlinux -nostdlib -nostartfiles HelloWorld.o -o helloWorld.so

test_jni.sh

export LD_LIBRARY_PATH=/home/ravindrak/exp
export CLASSPATH=.:
#/usr/java/jdk1.5.0_01/bin/java -Djava.library.path=.:/home/ravindrak/exp -classpath .: jni.test.HelloWorld
/usr/java/jdk1.5.0_01/bin/java -Djava.library.path=.:/home/ravindrak/exp jni.test.HelloWorld

.:/home/ravindrak/exp
Exception in thread "main" java.lang.UnsatisfiedLinkError: no helloWorld.so in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:992)
at jni.test.HelloWorld.<clinit>(HelloWorld.java:12)

But helloWorld.so file is available in the same location: /home/ravindrak/exp

The system is reporting the library path correctly but not loading the library.

Pls tell me the solution

Thanks and Regards,

Ravindra
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 11 2005
Added on Aug 4 2005
8 comments
286 views