Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JNI - (Java) Pass a String to C++/Return a String from C++

843829Aug 28 2001 — edited Aug 28 2001
I am trying to "Pass a string to C++" using JNI<~~~I am successful. If I "Return a string back to Java"<~~I am successful also. When I "pass a string" to a C++ function(within C++), I can't "return a string". To make it more understandable, using Java, I need to pass a String to C++. In C++, I need to pass the String that came from Java to a C++ function. The C++ function needs to return a String. I believe the problem is the C++ function is unable to return the String. Someone please HELP!!! Below is my C++ code:

#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include <jni.h>
#include "PassReturnString.h"

char returnString(char*);

JNIEXPORT jstring JNICALL Java_PassReturnString_showString
(JNIEnv *env, jobject obj, jstring jstr) {
static char copyStr[32];

const char* str = env->GetStringUTFChars(jstr, 0);
strcpy(copyStr, str);
env->ReleaseStringUTFChars(jstr, str);
const char ret = (char)returnString(copyStr);
return env->NewStringUTF(ret);
}
char returnString(char* c) {
char *back;

if(*c == '1') {
back = "Hello";
printf("%s", back);
return *back;
}
else if(*c == '2') {
back = "World";
printf("%s", back);
return *back;
}
else if(*c == '3') {
back = "!!!";
printf("%s", back);
return *back;
}
else {
back = "Error";
printf("%s", back);
return *back;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 25 2001
Added on Aug 28 2001
1 comment
1,527 views