【问题标题】:JNI UnsatisfiedLinkError for Ubuntu;Ubuntu 的 JNI UnsatisfiedLinkError;
【发布时间】:2013-06-11 18:10:21
【问题描述】:

我在 stackoverflow 上查看过类似的问题,但我发现的解决方案似乎都不适合我。我在 Linux/Ubuntu 机器上。我只是在练习 JNI,但出现此错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: nativetest.sayHello(Ljava/lang/String;)Ljava/lang/String;
at nativetest.sayHello(Native Method)
at nativetest.main(nativetest.java:8)

我已经提供了我的 .c、.h 和 .java 文件。

.java 文件:

public class nativetest
{
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("Hi");
 System.out.println("Invocation returned " + retval);
 }
}

.c 文件:

#include "nativetest.h"
#include <stdio.h>
#include <jni.h>
#include <stdlib.h>

JNIEXPORT jstring JNICALL Java_nativetest_sayHello(JNIEnv *env, jobject thisobject, jstring js)
{
  return js;
}

.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

我使用这些命令生成 .h 文件,编译/生成 .so 文件,然后运行:

javac nativetest.java

javah -jni nativetest

gcc --shared -o libnativetest.so -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/lib/jvm/java-7-openjdk-amd64/include/linux nativetest.c

java -Djava.library.path=。原生测试

我目前在包含 libnativetest.so 文件和所有 .c、.java 和 .h 文件的目录中。

任何帮助将不胜感激。

【问题讨论】:

    标签: java-native-interface unsatisfiedlinkerror


    【解决方案1】:

    好的,结果调用

    System.loadLibrary("nativetest");
    

    应该是静态的:

    static{
    System.loadLibrary("nativetest");
    }
    

    我在使用 javac 重新编译时也犯了错误。我对 Linux 很陌生 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多