Interaction between -Djava.library.path and -javaagent (bug or feature?)

I am using the -javaagent option to load a custom class transformer which

modifies a specified method by adding a call to a native library at beginning

and at the end. I have made exactly the same modification by hand and not

used the option -javaagent:... to prove that everything works OK and there

are no obvious problems. However, when I use the -javaagent option the

native library is not loaded properly:-

"Exception in thread "main" java.lang.UnsatisfiedLikeError: no xxx in java.library.path"

at java.lang.ClassLoader.loadLibrary (ClassLoader.java:1682)

etc.

To address this I have tried adding the option -Djava.library.path=/home/xxx

which makes no difference at all. A glance at the code for loadLibrary shows

that it will follow a different path if the class loader is null. With this in mind

I have added a print statement to my test class to show the object's class

loader. If I use the -javaagent option then it is null otherwise it is

sun.misc.Launcher$AppClassLoader@56169e11.

Is deliberate that the class loader should change when instrumentation is

used? If so is it deliberate that the loadLibrary function only looks in one

place when the class loader is null to me the two policies seem to make

a problem.

Is it deliberate that the message in the exception is hardwired not mention

that it also looks in jre/lib/sparcv9 ? I know for certain that it does because

I can work around the problem putting the library here.

Is there a more elegant work around than the one I have?

[1639 byte] By [869800jj_shorea] at [2007-11-26 12:18:23]
# 1

There are many reasons why a native library might not be loaded, all resulting in the same error unfortunately. Assuming this is Solaris or Linux, I'd look closely at the library using ldd and make sure the library is able to find all the extern symbols it needs and libraries it needs, on it's own. If that works then it should be loadable... Unless the JVM and your library have different library dependencies. You could run an ldd on the libjvm.so file you have and make sure the libraries match. With Solaris it's less likely there is an ordering problem, I'm not that sure with Linux, so you might also verify that the library orders look right, or that the order doesn't matter.

The -javaagent is for use with pure Java instrumentation for the most part, so how you are using it for instrumentation with a native library is very puzzling to me. I'm assuming that your native library is just doing the instrumentation? Or are you instrumenting bytecodes that call native methods?

And you haven't said how this library is loaded, are you calling System.loadLibrary()?

Also, you might try setting LD_LIBRARY_PATH to point at where your library is to see if that helps. Where agent libraries are found is different from normal JNI libraries. It's possible that the java.library.path doesn't really apply to the way your library is being loaded, how ever that is.

-kto

423397kellyohaira at 2007-7-7 14:57:52 > top of Java-index,Archived Forums,Socket Programming...