java 异常 JNI (Ljava/lang/String;)Ljava/lang/String;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3870043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:46:30  来源:igfitidea点击:

Exception JNI (Ljava/lang/String;)Ljava/lang/String;

javac++java-native-interface

提问by JN_newbie

I have made a little program in java that accepts a string as a user input. Now i have made a dll writing its code in Visual C++. when i run my program from netbeans it displays this exception.

我用java做了一个小程序,它接受一个字符串作为用户输入。现在我已经制作了一个用 Visual C++ 编写代码的 dll。当我从 netbeans 运行我的程序时,它显示此异常。

Exception in thread "main" java.lang.UnsatisfiedLinkError: Prompt.getLine(Ljava/lang/String;)Ljava/lang/String;
        at Prompt.getLine(Native Method)
        at Prompt.main(Prompt.java:19)

What problem is this. Need Help. Thanks

这是什么问题。需要帮忙。谢谢

This is java code

这是java代码

public class Prompt {

    /**
     * @param args the command line arguments
     */
    private native String getLine(String prompt);
    public static void main(String[] args) {
        // TODO code application logic here
        Prompt p = new Prompt();
        String input = p.getLine("Type a line:");
        System.out.println("User Typed:" + input);
    }
    static {
        System.loadLibrary("Prompt");
        //System.load("C:/Program Files/Java/jdk1.6.0/bin/Prompt.dll");
    }
}

and this is C++ Code

这是 C++ 代码

#include "stdafx.h"
#include "jni.h"
#include "jni_md.h"

JNIEXPORT jstring JNICALL Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)
{
    char buf[128];
    const char *str;
    str = env->GetStringUTFChars(prompt,0);
    if (str == NULL) {
        return NULL; /* OutOfMemoryError already thrown */
    }
    printf("%s", str);
    env->ReleaseStringUTFChars(prompt, str);
    /* We assume here that the user does not type more than
    * 127 characters */
    scanf("%s", buf);
    return env->NewStringUTF(buf);
}

采纳答案by JN_newbie

@org.life.java....i got the problem and it was my mistake, i was not including the header file of java which is JNI style header file which is Prompt.hin c++, "#include "jni_md.h" this will be eliminated and included "Prompt.h"now it is working fine.

@org.life.java ....我遇到了问题,这是我的错误,我没有包含java的头文件,它是Prompt.hC++中的JNI样式头文件,“ #include "jni_md.h”这将被消除并"Prompt.h"现在包含它工作正常。