3

So, I want to obtain resultNative from c++ but resultNative is in Java. can anybody please show me how to do that via JNI? I am not so familiar with c++ and have searched google for a long time but didn't find any answer. thank you so much. the nativeResult is here in Java.

public String[] searchDatabase()
{
    String result[]=new String[6];
    String nativeResult[]=new String[2];
    tName=this.getTargetName();

    result=da.SearchKorea(tName,ctx);
    tType=result[2];
    tTrans=result[3];
    tImage=result[4];
    tFave=result[5];

    nativeResult[1]= tImage+" - "+tName;
    nativeResult[2]= tTrans+" ["+tType+"]";
    return nativeResult;
}
2
  • You will have to do a call from C++. Create an instance of a JVM, find searchDatabase, and call it. This is the only way for C++ to CALL a java function. Otherwise, you'll need to call the C++ function from java and pass it the result of searchDatabase. Commented Jan 11, 2014 at 23:51
  • It is not clear from your text that you want to receive an array of Strings from Java for use in C code; would you like to edit your question? Actually, there are two different scenarios: provide String[] parameter to a C function from Java, or use searchDatabase() method as a callback from C code. Commented Jan 19, 2014 at 9:13

2 Answers 2

2

First you have to get a reference to the class providing the method. Let's say your class is called MyClass and it is in package p. You get the reference to the class like this:

// You get the JNIEnv* pointer when calling a native function.
jclass myClass = env->FindClass("p/MyClass");

Or if you have a reference to the java object then you can also use GetObjectClass:

jclass myClass = env->GetObjectClass(javaObject);

Then you have to get the ID of the method you'd like to call by providing the name of the method and a string describing the signature of the method.

"()[java/lang/String;" describes a method expecting no arguments and returning a string array.
jmethodID methodID = env->GetMethodID(myClass , "searchDatabase", "()[java/lang/String;");

Then you have to call the method with JNIEnv::CallObjectMethod, and here you have to pass the referenc to the java object.

jobjectarray strings = env->CallObjectMethod(javaObject, methodID);

Then you can get an element of the array with GetObjectArrayElement.

int index = 0;
jstring string = env->GetObjectArrayElement(strings, index);

And then you can get the native string from it in various ways.

const char* nativeChars = env->GetStringUTFChars(string, nullptr);

You can find more information about JNI here, and details about JNI type signatures here.

Sign up to request clarification or add additional context in comments.

Comments

0

I believe you could do it by passing back a jobjectArray.

int elements = 5;
jobjectArray returnArray = (jobjectArray)env->NewObjectArray(elements, env->FindClass("java/lang/string"), env->NewStringUTF(""));
for(int i = 0; i < elements; i++)
{
    char str[12];
    sprintf(str, "%i", i);
    env->SetObjectArrayElment(returnArray, i, env->NewStringUTF(str));
}

return returnArray;

I've only done this with a byte array

2 Comments

I mean, my c++ needs the nativeResult from java, how can I make a function in c++ to get the nativeResult from java?
Your JNI code would take in a jobjectArray as one of its parameters. You can use the env->GetObjectArrayElement to get a single jobject item. Cast it to a jString and convert it to a c string with env->GetStringUTFChars(env, STRING_VAR_HERE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.