HELP: java.lang.UnsatisfiedLinkError: no ... in java.library.path
843829Jan 29 2007 — edited May 5 2008am the beginner of JNI. I write a test program to use C code in JAVA.
[yxz155@lionxo JAVAaC]$ ls
AClassWithNativeMethods.java theNativeMethod.c
[yxz155@lionxo JAVAaC]$ javac AClassWithNativeMethods.java
[yxz155@lionxo JAVAaC]$ javah -jni AClassWithNativeMethods
[yxz155@lionxo JAVAaC]$ ls
AClassWithNativeMethods.class AClassWithNativeMethods.java
AClassWithNativeMethods.h theNativeMethod.c
[yxz155@lionxo JAVAaC]$ gcc -c -fPIC -I/usr/global/java/include -I/usr/global/java/include/linux theNativeMethod.c
[yxz155@lionxo JAVAaC]$ ls
AClassWithNativeMethods.class AClassWithNativeMethods.java theNativeMethod.o
AClassWithNativeMethods.h theNativeMethod.c
[yxz155@lionxo JAVAaC]$ ld -G theNativeMethod.o -o libJCI.so -lm -lc -lpthread
[yxz155@lionxo JAVAaC]$ ls
AClassWithNativeMethods.class AClassWithNativeMethods.java theNativeMethod.c
AClassWithNativeMethods.h libJCI.so theNativeMethod.o
[yxz155@lionxo JAVAaC]$ java AClassWithNativeMethods
Exception in thread "main" java.lang.UnsatisfiedLinkError: no CJI 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 AClassWithNativeMethods.<clinit>(AClassWithNativeMethods.java:9)
[yxz155@lionxo JAVAaC]$
The codes are as follows, I tried setProperty() as in java program, but it did not work. I also tried set LD_LIBRARY_PATH and it did not work either.
//AClassWithNativeMethods.java
import java.io.File;
import java.lang.System;
public class AClassWithNativeMethods{
public native void theNativeMethod();
static {
//System.setProperty("java.library.path",System.getProperty("java.library.path")+ File.pathSeparator+"/home2/yxz155/JAVAaC");
//System.out.println(System.getProperty("java.library.path"));
System.loadLibrary("CJI");
}
public static void main(String[] args){
AClassWithNativeMethods test = new AClassWithNativeMethods ();
test.theNativeMethod();
}
}
// theNativeMethod.c
#include <stdio.h>
#include "AClassWithNativeMethods.h"
JNIEXPORT void JNICALL Java_AClassWithNativeMethods_theNativeMethod
(JNIEnv *env, jobject obj){
printf("Hello~~~~");
}
//AClassWithNativeMethods.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class AClassWithNativeMethods */
#ifndef IncludedAClassWithNativeMethods
#define IncludedAClassWithNativeMethods
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: AClassWithNativeMethods
* Method: theNativeMethod
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_AClassWithNativeMethods_theNativeMethod
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif