Xcode creation JVM

oimy

Membre confirmé
29 Mai 2018
14
0
42
Bonjour,

J'ai fait un programme permettant de lancer une interface java à partir du C++ :

Bloc de code:
// Example 2 of JNI invocation.
// The java environment is prepared.  Errors are reported.
// Call to a simple static java methods
// License: ZLIB license (see license.txt)
// (c) Copyright 2015 by cth027

#include <iostream>
#include <jni.h>
#include <objc/objc-runtime.h>

void runCocoaMain(void)
{
    id clazz = (id) objc_getClass("NSApplication");
    id _Nullable app = objc_msgSend(clazz, sel_registerName("sharedApplication"));
   
    objc_msgSend(app, sel_registerName("run"));
}


int main()
{
    using namespace std;
   
    JavaVM *jvm;                // Pointer to the JVM (Java Virtual Machine)
    JNIEnv *env;                // Pointer to native interface
   
    //==================== prepare loading of Java VM ============================
   
    JavaVMInitArgs vm_args;                        // Initialization arguments
    JavaVMOption options[1];   // JVM invocation options
    options[0].optionString = strdup("-Djava.class.path=.");   // where to find java .class
    vm_args.version = JNI_VERSION_1_8;             // minimum Java version
    vm_args.nOptions = 1;                          // number of options
    vm_args.options = options;
    vm_args.ignoreUnrecognized = JNI_TRUE;     // invalid options make the JVM init fail
   
    //================= load and initialize Java VM and JNI interface ===============
    printf("Avant blocage\n");
    jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);  // YES !!
   
    //========================= analyse errors if any  ==============================
    // if process interuped before error is returned, it's because jvm.dll can't be
    // found, i.e.  its directory is not in the PATH.
    if (rc == JNI_ERR) {
        printf("All done, bye bye!\n");
    }
    if(rc != JNI_OK) {
        if(rc == JNI_EVERSION)
            cerr << "FATAL ERROR: JVM is oudated and doesn't meet requirements" << endl;
        else if(rc == JNI_ENOMEM)
            cerr << "FATAL ERROR: not enough memory for JVM" << endl;
        else if(rc == JNI_EINVAL)
            cerr << "FATAL ERROR: invalid ragument for launching JVM" << endl;
        else if(rc == JNI_EEXIST)
            cerr << "FATAL ERROR: the process can only launch one JVM an not more" << endl;
        else
            cerr << "FATAL ERROR:  could not create the JVM instance (error code " << rc << ")" << endl;
        cin.get();
        exit(EXIT_FAILURE);
    }
   
    cout << "JVM load succeeded. \nVersion ";
    jint ver = env->GetVersion();
    cout << ((ver >> 16) & 0x0f) << "." << (ver & 0x0f) << endl;
   
    // First call to JAVA ==================================================================
    jclass cls2 = env->FindClass("MyTest2");  // try to find the class
    if(cls2 == nullptr) {
        cerr << "ERROR: class not found !";
    }
    else {                                  // if class found, continue
        cout << "Class MyTest found" << endl;
        //       runCocoaMain();
        jmethodID mid = env->GetStaticMethodID(cls2, "run", "()V");  // find method
        if(mid == nullptr)
            cerr << "ERROR: method void mymain() not found !" << endl;
        else {
            cout << "===Call to java==================" << endl;
           
            env->CallStaticVoidMethod(cls2, mid);                      // call method
            runCocoaMain();
            cout << "===End of call to java==========="<<endl;
        }
    }
    // End JAVA calls ==================================================================
   
   
    jvm->DestroyJavaVM();
   
    cout << "Press any key...";
    cin.get();
}

Le programme fonctionne parfaitement quand je le compile et l'exécute en ligne de commande.
Par contre il bloque sur le thread principale quand je l'exécute sous Xcode.

Voici le message d'erreur avec la commande bt : " thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSEGV
* frame #0: 0x00000001047b52b4"

Dans le debugger de Xcode j'ai "Thread 1: signal SIGSEGV" sur la ligne "0x1047b52b4: movl (%rsi), %eax"

Dans la fenêtre d'affichage de Xcode j'ai l'affichage de "printf("Avant blocage\n");" puis il y a écrit "lldb" en bleu clair.

D'où peut venir mon problème ? Est-ce une config à faire dans Xcode ? Est-ce parce que %rsi est en 64bits et que %eax est en 32bits ?

Merci d'avance