珍妮不工作了

Jni not working

本文关键字:工作      更新时间:2023-10-16

这是我的Java代码。

class NativePrompt {
    private native String getInput(String prompt);  //native method
    static   //static initializer code
    {
        System.loadLibrary("NativePrompt");
    } 
    public static void main(String[] args)
    {
        NativePrompt NP = new NativePrompt();
        String sName = NP.getInput("Enter your name: ");
        System.out.println("Hello " + sName);
    }
}

我使用jdk1.7.0_17。这是我的c++代码

#include "NativePrompt.h" 
#include "jni.h"
#include "string"
#include "iostream"
#include "vector"
using namespace std;
/*
 * Class:     NativePrompt
 * Method:    getInput
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_NativePrompt_getInput
    (JNIEnv *env, jobject obj, jstring prompt){
    string sEntry;
    const char *str;
    str = env->GetStringUTFChars(prompt, NULL);
    if (str == NULL) {
        return env->NewStringUTF("");
    }
    else{
    cout << str;
        //Frees native string resources
        env->ReleaseStringUTFChars(prompt, str);
        //reads n-consecutive words from the 
        //keyboard and store them in string
        getline(cin, sEntry);
        return env->NewStringUTF(sEntry.c_str());
    }
}

我使用下面的注释运行这个程序。

javac NativePrompt.java

javah NativePrompt

g++ -o NativePrompt。所以-shared -I/usr/lib/jvm/jdk1.7.0_17/include/usr/lib/jvm/jdk1.7.0_17/include/linux NativePrompt.cpp

出口LD_LIBRARY_PATH = '/home/user/jniwork/'

java NativePrompt

现在我得到下面的错误。我不知道如何解决这个问题。

线程"main"异常java.lang.UnsatisfiedLinkError: noNativePrompt in java.library.path atjava.lang.ClassLoader.loadLibrary (ClassLoader.java: 1860)java.lang.Runtime.loadLibrary0 (Runtime.java: 845)java.lang.System.loadLibrary (System.java: 1084)NativePrompt。(NativePrompt.java: 5)

尝试像这样启动应用程序:

java -Djava.library.path=/home/user/jniwork/ NativePrompt

以及之前,从NativePrompt重命名您的库。libNativePrompt.so