Name: vsR10008 Date: 08/07/2000
System.loadLibrary fails to load native library when loadLibrary is invoked
from class loaded by bootstrap classloader. Let's consider the following example:
--------------------------------------------------------------------- t.java --
public class t {
static {
System.out.println("Class Loader:"+(t.class.getClassLoader()==null?"BOOTSTRAP":"APPLICATION"));
System.out.println("java.library.path=\""+System.getProperty("java.library.path")+"\"");
System.loadLibrary("t");
}
public static void main(String[] argv) {
n();
}
static native void n();
}
--------------------------------------------------------------------- t.c --
#include <jni.h>
#include <stdio.h>
JNIEXPORT void JNICALL Java_t_n
(JNIEnv *env, jclass cls) {
printf("Hello,world!\n");
}
----------------------------------------------------------------------------
This test should print out:
1) which classloader is used to load this class;
2) property "java.library.path" that contains library search path
3) message from native method n()
To make JVM load class t with bootstrap classloader instead of application
classloader we use the option -Xbootclasspath/p: or -Xbootclasspath/a:
Execution script:
> uname -a
SunOS novo12 5.7 Generic_Patch sun4u sparc SUNW,Ultra-2
> mkdir lib
> setenv LD_LIBRARY_PATH lib
> mkdir classes
> cc -G -KPIC -o lib/libt.so -I${JDK}/include -I${JDK}/include/solaris t.c
> javac -d classes t.java
> java -version
java version "1.4.0beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0beta-b26)
Java HotSpot(TM) Core VM (build 1.3-internal, interpreted mode)
> java -classpath classes t
Class Loader:APPLICATION
java.library.path="/export/ld25/java/dest/jdk1.4.0beta-b26/solaris/bin/../jre/lib/sparc:/export/ld25/java/dest/jdk1.4.0beta-b26/sola
ris/bin/../jre/lib/sparc/hotspot:/export/ld25/java/dest/jdk1.4.0beta-b26/solaris/bin/../jre/lib/sparc/5.7:/export/ld25/java/dest/jdk
1.4.0beta-b26/solaris/bin/../jre/../lib/sparc:lib:/usr/lib"
Hello,world!
> java -Xbootclasspath/a:classes t
Class Loader:BOOTSTRAP
java.library.path="/export/ld25/java/dest/jdk1.4.0beta-b26/solaris/bin/../jre/lib/sparc:/export/ld25/java/dest/jdk1.4.0beta-b26/sola
ris/bin/../jre/lib/sparc/hotspot:/export/ld25/java/dest/jdk1.4.0beta-b26/solaris/bin/../jre/lib/sparc/5.7:/export/ld25/java/dest/jdk
1.4.0beta-b26/solaris/bin/../jre/../lib/sparc:lib:/usr/lib"
Exception in thread "main" java.lang.UnsatisfiedLinkError: no t in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1312)
at java.lang.Runtime.loadLibrary0(Runtime.java:749)
at java.lang.System.loadLibrary(System.java:829)
at t.<clinit>(t.java:4)
> java -classpath classes -Djava.library.path=lib t
Class Loader:APPLICATION
java.library.path="lib"
Hello,world!
> java -Xbootclasspath/p:classes -Djava.library.path=lib t
Class Loader:BOOTSTRAP
java.library.path="lib"
Exception in thread "main" java.lang.UnsatisfiedLinkError: no t in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1312)
at java.lang.Runtime.loadLibrary0(Runtime.java:749)
at java.lang.System.loadLibrary(System.java:829)
at t.<clinit>(t.java:4)
This bug affects both classic and HotSpot in jdk1.2,jdk1.2.1,jdk1.2.2,jdk1.3,jdk1.4
======================================================================