I am trying to pass a String to a native c
function but the String gets printed as 09▀30≈ß#@÷g
for the String Type a String :
. I don't know what the problem is .
This is the code that i have used to do this :
JAVA CODE
class Prompt {
private native String myGetLine( String prompt ); // native method that prints a prompt and reads a line
public static void main( String args[] ) {
Prompt prompt = new Prompt();
String input = prompt.myGetLine( "Type a String : ");
System.out.println("User Typed : " + input);
}
static {
System.loadLibrary("StringPassing"); // StringPassing.dll
}
}
C CODE
#include <jni.h>
#include <stdio.h>
#include "D:\UnderTest\JNI\StringPassing\Debug\Prompt.h"
JNIEXPORT jstring JNICALL Java_Prompt_myGetLine(JNIEnv *env, jobject obj, jstring str) {
char buf[128];
const jbyte *str_JVM;
str_JVM = (*env) -> GetStringUTFChars( env , str , NULL);
if( str_JVM == NULL ) return NULL;
printf("%s ",str);
(*env)->ReleaseStringUTFChars( env , str , str_JVM);
scanf("%s",buf);
return (*env)->NewStringUTF( env , buf);
}
The screen after the program ends looks like :
D:\UnderTest\JNI\StringPassing\Debug>java Prompt
09▀30≈ß#@÷gV
User Typed : V
Why don't i get the string Type a String :
on cmd when i run the program?
Also: my compiler underlines (*env)
and points out that Expression must have pointer type. But when I compile I don't get any errors. Why is that and what is it?
You pass str
to printf
when I'm pretty sure that you wanted to pass str_JVM
to it instead.
str_JVM = (*env) -> GetStringUTFChars( env , str , NULL);
if( str_JVM == NULL ) return NULL;
printf("%s ",str_JVM);