-3

可能重复:
如何在从 JNI 调用的代码中捕获 System.exit()

JNI CallStaticVoidMethod调用后正确启动Java进程但不返回C++代码,然后退出。我的java code没有,system.exit(0)但只有return;

jclass StringClass = m_JVMEnv->FindClass("java/lang/String");
    int numOfArguments = (int)m_ServerArguments.size() + 4 ;
    int argumentIndex = 0;

    jobjectArray jargs = m_JVMEnv->NewObjectArray(numOfArguments, StringClass, NULL);    

    m_JVMEnv->SetObjectArrayElement(jargs, argumentIndex++, m_JVMEnv->NewStringUTF("start"));

    std::string strJavaClassPath = "-classpath=";
    strJavaClassPath             += "\"";
    for ( std::size_t idx = 0; idx < m_listOfJars.size() - 1 ; idx++) {
        strJavaClassPath += m_ProductLibDir + "\\lib\\" + m_listOfJars[idx] + ";";
    }
    strJavaClassPath      += m_ProductLibDir + "\\lib\\" + m_listOfJars[m_listOfJars.size() - 1] ;
    strJavaClassPath      += "\"";

    m_JVMEnv->SetObjectArrayElement(jargs, argumentIndex++, m_JVMEnv->NewStringUTF(strJavaClassPath.c_str()));

    for ( std::vector<std::string>::iterator iter = m_ServerArguments.begin(); iter != m_ServerArguments.end(); ++iter) {
        std::string argument = *iter;
        m_JVMEnv->SetObjectArrayElement(jargs, argumentIndex++, m_JVMEnv->NewStringUTF(argument.c_str()));
    }
    m_JVMEnv->SetObjectArrayElement(jargs, argumentIndex, m_JVMEnv->NewStringUTF("-dir=E:\\Avinash\\personal\\work\\CreateJvm\\Debug"));    

    m_JVMEnv->CallStaticVoidMethod(m_CacheServerLauncherClass, m_MainMethodID, jargs);

    std::cout << "I am done with launching java program" << std::endl;
    m_RunningJVMInstance->DestroyJavaVM();
    CheckForJNIException();

m_JVMEnv->CallStaticVoidMethod(m_CacheServerLauncherClass, m_MainMethodID, jargs);此行之后程序控制不返回。

4

1 回答 1

1

您的 Java 代码将需要一个线程来运行,如果您想要另一个线程用于您的 C++ 代码,则必须创建一个。

于 2012-07-09T13:00:46.213 回答