[英]jni String to *char and java.lang.UnsatisfiedLinkError:
我試圖用JNI運行簡單的程序,在我嘗試使用Strings之前,一切都運行良好:
hello.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
即時通訊編譯:
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
中hello.c
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.