jni String to *char and java.lang.UnsatisfiedLinkError:

jni String to *char and java.lang.UnsatisfiedLinkError:

本文关键字:java lang UnsatisfiedLinkError and char String to jni      更新时间:2023-10-16

im 尝试使用 JNI 运行简单的程序,在我尝试使用字符串之前一切都运行良好:

您好.java:

public class hello {
  public native String getLine(String name) ;
  public static void main (String args[]) {
    String str = "Pawel" ;
    hello h = new hello () ;
    System.out.println(h.getLine(str)) ;
  }
  static {
    System.loadLibrary ( "hello" ) ;
  }
}

你好:

#include<stdio.h>
#include<jni.h>
#include "hello.h" 
JNIEXPORT jstring JNICALL 
Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)
 {
   char buf[128];
   const jbyte *str;
   str = (*env)->GetStringUTFChars(env, prompt, NULL);
   if (str == NULL) {
       return NULL; 
   }
   printf("%s", str);
   (*env)->ReleaseStringUTFChars(env, prompt, str);
   scanf("%s", buf);
   return (*env)->NewStringUTF(env, buf);
 }

你好:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class hello */
#ifndef _Included_hello
#define _Included_hello
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jstring JNICALL Java_hello_getLine
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif

IM 编译:

gcc -fPIC -I /usr/lib/jvm/java-7-openjdk-amd64/include -I /usr/lib/jvm/java-7-openjdk-amd64/include/linux -shared -o libhello.so hello.c

并运行:

java -Djava.library.path=. hello

输出:

Exception in thread "main" java.lang.UnsatisfiedLinkError: hello.getLine(Ljava/lang/String;)Ljava/lang/String;
    at hello.getLine(Native Method)
    at hello.main(hello.java:9) //it points to native function call

怎么了?

Java_Prompt_getLine函数重命名为Java_hello_getLine in hello.c

相关文章: