CallObjectMethod returns no string

My goal is to instantiate a class Java

in C++

and then call the methods defined in the class Java

.

Here's a summary of my class Java

:

EXICodec.java

public class EXICodec {
    ...
    private static String inputXML;
    ...
    public EXICodec()
    {
        System.out.println("Constructor");  
        this.inputXML = "string";
        ...
    }
    ...
    public static void setInputXML(String inXML)
    {
        inputXML = inXML;
    }
    ...
    public static  String getInputXML()
    {
        return inputXML;
    }

      

Here's a quick summary of the code C++

used to interact with this class:

JavaInterface.hh

class JavaInterface
{
    private:
        JNIEnv*     JNIEnvironment;
        JavaVM*     javaVM;
        jclass      javaClass;
        jobject     javaClassInstance;

        jmethodID   IDconstructor;
        jmethodID   IDsetInputXML;
        jmethodID   IDgetInputXML;

    public:
        JavaInterface();                    
        void init_context( void);               
        void init_class( void);
        void init_methods( void);                   

        void setInputXML( std::string);
        std::string getInputXML( void);
}

      

JavaInterface.cc

void JavaInterface::init_context(){..}    //initiate the JNIEnvironment & javaVM attributes
void JavaInterface::init_class(){..}      //initiate the javaClass attribute
void JavaInterface::init_methods()
{
     this->IDconstructor = this->JNIEnvironment->GetMethodID(this->javaClass, "<init>", "()V");
     if (this->IDconstructor == NULL) 
     {
         throw std::runtime_error("JAVA_INIT_METHOD_EXCEPTION");
     }

     this->IDsetInputXML = this->JNIEnvironment->GetStaticMethodID(this->javaClass, "setInputXML", "(Ljava/lang/String;)V");
     if (this->IDsetInputXML == NULL) 
     {
         throw std::runtime_error("JAVA_INIT_METHOD_EXCEPTION");
     }

     this->IDgetInputXML = this->JNIEnvironment->GetStaticMethodID(this->javaClass, "getInputXML", "()Ljava/lang/String;");

     if (this->IDgetInputXML == NULL) 
     {
         throw std::runtime_error("JAVA_INIT_METHOD_EXCEPTION");
     }
}      
void JavaInterface::j_constructor()
{
    this->javaClassInstance = this->JNIEnvironment->NewObject(this->javaClass, this->IDconstructor);      
    if (this->javaClassInstance == NULL) 
    {
        throw std::runtime_error("JAVA_CONSTRUCTOR_CALL_EXCEPTION");
    }
}

void JavaInterface::j_setInputXML( std::string str)
{   
    jstring argument = this->JNIEnvironment->NewStringUTF(str.c_str());
    this->JNIEnvironment->CallVoidMethod(this->javaClassInstance, this->IDsetInputXML, argument);
}

std::string JavaInterface::j_getInputXML()
{
    jstring javaString = NULL;

    // ---> PROBLEM HERE : javaString is still NULL after the line below : <---
    javaString = (jstring)this->JNIEnvironment->CallObjectMethod(this->javaClassInstance, this->IDgetInputXML, 0);  

    // jString to char*
    const char *nativeString = this->JNIEnvironment->GetStringUTFChars(javaString, JNI_FALSE);
    // char* to std::string
    std::string str(nativeString);

    this->JNIEnvironment->ReleaseStringUTFChars(javaString, nativeString);

    return str;
}   

      

So my problem is that the call CallObjectMethod

doesn't return anything. I tried to change the Java method to static

, but it didn't change.

In general, how would you return a string from Java

to C++

? Maybe my starting idea is bad.

+3


source to share


1 answer


So, sorry for taking so long, but I solved the problem. Basically, this is a small typo causing havoc (at least in your MWE):

 this->IDgetString = this->JNIEnvironment->GetStaticMethodID(this->javaClass
                                    , "getString"
                                    , "(()Ljava/lang/String;");
                                    // ^ This parenthesis must be removed.

      

Also, I noticed that you did not initialize the object. I ditched your code to use instace methods instead of static methods and MWE works fine for me.

Here is the code for EXICodec.java

I used and here is the code for JavaInterface.cpp

.



I looked at the original code again and found something else.

void JavaInterface::init_methods()
{
    this->IDconstructor = this->JNIEnvironment->GetMethodID(this->javaClass, "<init>", "()V");
    if (this->IDconstructor == NULL) 
    {
        throw std::runtime_error("JAVA_INIT_METHOD_EXCEPTION");
    }

    this->IDsetInputXML = this->JNIEnvironment->GetStaticMethodID(this->javaClass, "setInputXML", "(Ljava/lang/String;)V");
    if (this->IDsetInputXML == NULL) 
    {
        throw std::runtime_error("JAVA_INIT_METHOD_EXCEPTION");
    }

    this->IDgetInputXML = this->JNIEnvironment->GetStaticMethodID(this->javaClass, "getInputXML", "()Ljava/lang/String;");
//} <- This brace must be further down
    if (this->IDgetInputXML == NULL) 
    {
        throw std::runtime_error("JAVA_INIT_METHOD_EXCEPTION");
    }
} // <- To here

      

I'm sure this is just a typo, but if not, it might be causing the problems in your original example.

+3


source







All Articles