2010-10-06 2 views
2

J'ai créé un petit programme en Java qui accepte une chaîne comme entrée utilisateur. Maintenant, j'ai fait un dll en écrivant son code dans Visual C++. quand j'exécute mon programme depuis netbeans, il affiche cette exception.Exception JNI (Ljava/lang/String;) Ljava/lang/String;

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) 

De quel problème s'agit-il? Besoin d'aide pour. Merci

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

et c'est C++ code

#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); 
} 
+0

avez-vous chargé dll? Pouvez-vous envoyer votre code. –

+0

ouais je l'ai chargé, bien sûr je vais le poster –

+0

'System.loadLibrary (" Prompt ");' ne devrait-il pas être le chemin de DLL le même que commenté? –

Répondre

1

@ org.life.java .... je suis le problème et ce fut mon erreur, j'étais ne comprend pas le fichier d'en-tête de java qui est le fichier d'en-tête de style JNI qui est Prompt.h en C++, "#include "jni_md.h" ceci sera éliminé et inclus "Prompt.h" maintenant il fonctionne bien.