1

我第一次尝试本机方法....
我从这个链接中进行了一个简单的编程点击...。

               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

已成功创建 nativetest.h 文件

                  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 代码

                          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

已成功创建共享对象文件。

当我执行 nativetest 时,它显示以下错误

java -Djava.library.path=。线程“主”java.lang.UnsatisfiedLinkError 中的nativetest
异常:java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
处 java.lang.Runtime.loadLibrary0(Runtime.java:845 ) 的 java.library.path 中没有 nativetest )
在 java.lang.System.loadLibrary(System.java:1084)
在 nativetest.(nativetest.java:4)

提前谢谢....

4

1 回答 1

2

在 Linux 上,共享库的文件名应以“lib”开头,即“lib[library_name].so”。

参考:3.1.1。共享库名称

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''.

于 2013-03-20T06:17:27.943 回答