Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <jni.h>
- using namespace std;
- int main() {
- JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine)
- JNIEnv *env; // Pointer to native interface
- //================== prepare loading of Java VM ============================
- // Initialization arguments
- JavaVMInitArgs vm_args;
- JavaVMOption *options = new JavaVMOption[1]; //JVM invocation options
- // where to find java.class
- options[0].optionString = (char *) "-Djava.class.path=U:/HelloJavaWorld/bin";
- vm_args.version = JNI_VERSION_1_6; // minimum Java version
- vm_args.nOptions = 1; // number of options
- vm_args.options = options;
- vm_args.ignoreUnrecognized = false; // invalid options make the JVM init fail
- //=============== load and initialize Java VM and JNI interface =============
- jint rc = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args); // YES !!
- delete options; // we then no longer need the initialization options.
- if (rc != JNI_OK) {
- // TO DO: error processing...
- cin.get();
- exit(EXIT_FAILURE);
- }
- //=============== Display JVM version =======================================
- cout << "JVM load succeeded: Version ";
- jint ver = env->GetVersion();
- cout << ((ver >> 16) & 0x0f) << "." << (ver & 0x0f) << endl;
- jclass cls2 = env->FindClass("HelloJavaWorld"); // try to find the class
- if (cls2 == nullptr) {
- cerr << "ERROR: class not found !";
- } else { // if class found, continue
- std::cout << "Class MyTest found" << endl;
- jmethodID mid = env->GetStaticMethodID(cls2, "sayhi", "()V"); // find method
- if (mid == nullptr) {
- cerr << "ERROR: method not found !" << endl;
- } else {
- env->CallStaticVoidMethod(cls2, mid); // call method
- cout << endl;
- }
- jvm->DestroyJavaVM();
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement