0

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" ) ;
  }
}

こんにちはC:

#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

どうしたの?

4

1 に答える 1

1

Java_Prompt_getLine関数の名前をJava_hello_getLinein に変更しますhello.c

于 2013-11-13T10:19:31.510 に答える