vendredi 29 mai 2015

CallObjectMethod does not return a string

My aim is to create an instance of a Java class in C++, and then call methods defined in the Java class.

Here is a summary of my Java class :

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 is a summary of the C++ code used to interact with that 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 of CallObjectMethod does not return anything. I tried to change the Java method to static but it did not change.

More generally, how would you return a string from Java to C++? Maybe my starting idea is bad.

Aucun commentaire:

Enregistrer un commentaire