le code simple cpp:
[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
JavaVMInitArgs vm_args; // Initialization arguments
JavaVMOption* options = new JavaVMOption[1]; // JVM invocation options
options[0].optionString = "-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
jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); // YES !!
delete options; // we then no longer need the initialisation options.
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;
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;
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;
}
}
jvm->DestroyJavaVM();
cout << "Press any key...";
cin.get();
}
[/code]