tags:

views:

824

answers:

2

I have a library called HelloWorld.so and a program HelloWorld.java with this content:

class HelloWorld {
     private native void print();
     public static void main(String[] args) {
         new HelloWorld().print();
     }
     static {
         System.loadLibrary("HelloWorld");
     }
 }

Now when I try to run HelloWorld.java I get this error:

$ /usr/java1.4/bin/java HelloWorld
Exception in thread "main"
java.lang.UnsatisfiedLinkError: no HelloWorld in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1491)
        at java.lang.Runtime.loadLibrary0(Runtime.java:788)
        at java.lang.System.loadLibrary(System.java:834)
        at HelloWorld.<clinit>(HelloWorld.java:7)

Any tips?

+6  A: 

Where is HelloWorld.so located? You probably need to specify its parent directory using the command-line parameter "-Djava.library.path".

For example, if it's in "/path/libs/HelloWorld.so", add -Djava.library.path=/path/libs as an option when invoking java. For instance, it's "-Djava.library.path=lib" on one of my projects.

Edit: Dan Dyer points out that the environment variable LD_LIBRARY_PATH also can be used for this.

Michael Myers
Does the .so extension need to be explicitly stated also?
Otis
Edited to clarify (it's actually the path to the folder containing the .so file).
Michael Myers
@erickson: Thanks, that does look better.
Michael Myers
An alternative is to set the LD_LIBRARY_PATH environment variable.
Dan Dyer
A: 

@mmyers Thank you for responding. We found out that all we had to do was change System.loadLibrary to System.load and pass the full path + filename as argument, worked like a charm.

Even before doing so, we tried using the "-D" parameter and setting LD_LIBRARY_PATH but we weren't successful.

Go figure! :)

Thanks again, Karen

KNewton
That's exceedingly strange. loadLibrary should behave identically, except that it requires the library to be on the path. Which is what the -Djava.libary.path is supposed to help with.
Michael Myers
Don't I know it.... I'd love it if someday someone could explain :)
KNewton