Question

I am trying native methods for the first time....
I had taken a simple programming from this link Click....

               nativetest.java
         public class nativetest
         {
            static {
              System.loadLibrary("nativetest");
            }
            public native String sayHello(String s);
            public static void main(String[] argv)
            {
                 String retval = null;
                 nativetest nt = new nativetest();
                 retval = nt.sayHello("Beavis");
                 System.out.println("Invocation returned " + retval);
            }

         }

javac nativetest.java
javah -jni nativetest

Successfully nativetest.h file has been created

                  nativetest.h
          /* DO NOT EDIT THIS FILE - it is machine generated */
          #include <jni.h>
          /* Header for class nativetest */

          #ifndef _Included_nativetest
          #define _Included_nativetest
          #ifdef __cplusplus
          extern "C" {
          #endif
          /*
           * Class:     nativetest
           * Method:    sayHello
           * Signature: (Ljava/lang/String;)Ljava/lang/String;
          */
          JNIEXPORT jstring JNICALL Java_nativetest_sayHello(JNIEnv *, jobject, jstring);

          #ifdef __cplusplus
          }
          #endif
     #endif

nativetest.c code

                          nativetest.c 
          include "nativetest.h"    /*double quotes tells it to search current directory*/

          JNIEXPORT jstring JNICALL Java_nativetest_sayHello (JNIEnv *env, jobject thisobject, jstring js)

          {
               return js;
          }

gcc -I/usr/java/jdk1.7.0_13/include -I/usr/java/jdk1.7.0_13/include/linux -o nativetest.so -shared nativetest.c

Successfully shared object file has been created.

when i executed the nativetest, it is showing the following error

java -Djava.library.path=. nativetest
Exception in thread "main" java.lang.UnsatisfiedLinkError: no nativetest in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at nativetest.(nativetest.java:4)

Thanx in advance....

Was it helpful?

Solution

On Linux, a shared library's file name should start with "lib", that is, "lib[library_name].so".

Reference: 3.1.1. Shared Library Names

Every shared library has a special name called the ``soname''. The soname has the prefix ``lib'', the name of the library, the phrase ``.so'', followed by a period and a version number that is incremented whenever the interface changes (as a special exception, the lowest-level C libraries don't start with ``lib''). A fully-qualified soname includes as a prefix the directory it's in; on a working system a fully-qualified soname is simply a symbolic link to the shared library's ``real name''.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top