简体   繁体   中英

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

im trying to run simple program with JNI, everything was working perfectly before i tried with 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" ) ;
  }
}

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

hello.h:

/* 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 compiling with:

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

and running with:

java -Djava.library.path=. hello

output:

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

What's wrong?

在重命名Java_Prompt_getLine功能Java_hello_getLinehello.c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM