0

I am trying to build and run an example jni program. The program is just a sample helloworld program. I did not write it but I assume that it works. I am running this on Linux. There are four files.

HelloNative.c  
HelloNative.h  
HelloNative.java  
HelloNativeTest.java  

To build the files, I did

gcc -I/myDir/jdk/include -I/myDir/jdk/include/linux -fPIC -c HelloNative.c  
gcc -shared -o HelloNative.so HelloNative.o  
java *java

Here is the result of the build

HelloNative.c  
HelloNative.h  
     HelloNative.o  
   HelloNativeTest.class  
HelloNative.class  
HelloNative.java  
HelloNative.so  
HelloNativeTest.java

Then I did

setenv LD_LIBRARY_PATH /myDir/myExample:${LD_LIBRARY_PATH}  
java HelloNativeTest

I got the following error

Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloNative in java.library.path  
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)  
        at java.lang.Runtime.loadLibrary0(Runtime.java:823)  
        at java.lang.System.loadLibrary(System.java:1028)  
        at HelloNative.<clinit>(HelloNative.java:9)  
        at HelloNativeTest.main(HelloNativeTest.java:8)  

I checked the LD_LIBRARY_PATH and the HelloClassTest and HelloNative.so, they were all there. I tried to specify the -CLASSPATH also, but that did not seem to matter. Does anyone have any ideas ?

3
  • First of all, you shouldn't have the C files have the same name as the Java files. There's Commented Oct 7, 2010 at 15:05
  • The files were from a tutorial package. I did not modify or rename them. I assume they work as is. I could try to modify and rename them. Are you sure that is the problem ? Commented Oct 7, 2010 at 15:13
  • It might be a good idea to tell people where they can download the sample from so they might try it. Also have you modified anything at all from the sample ? Commented Oct 7, 2010 at 21:47

3 Answers 3

1

Do the following, where X="HelloNative".

  • Give the library a filename following a system-dependent standard. On Linux, name your library libX.so.
  • Set the java.library.path System property to the directory containing your library.
  • Call System.loadLibrary("X") where "X" is the cross-platform part of the library name above.

You named your library HelloNative.so; change it to libHelloNative.so.

From http://download.oracle.com/javase/6/docs/technotes/guides/jni/spec/design.html#wp679:

The argument to System.loadLibrary is a library name chosen arbitrarily by the programmer. The system follows a standard, but platform-specific, approach to convert the library name to a native library name. For example, a Solaris system converts the name pkg_Cls to libpkg_Cls.so, while a Win32 system converts the same pkg_Cls name to pkg_Cls.dll.

If you use OSGi in the future, there's an alternative to setting java.library.path.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, thank you Andy. Changing the HelloNative.so to libHelloNative.so did the trick.
0

You can also try by setting the java.library.path:

java -Djava.library.path=/myDir/myExample HelloNativeTest

1 Comment

That option did not make any difference.
0

Did you do a System.loadLibrary() from Java?

2 Comments

Like I said this is straight from Sun Java tutorial package. I assume they know what they are doing. I did check and they did have class HelloNative { public static native void greeting(); static { System.loadLibrary("HelloNative"); } }
Libraries on Linux are expected to begin with "lib". Try renaming to libHelloNative.so?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.