SHARE
TWEET

Untitled

a guest Feb 19th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int main()
  2. {
  3.  
  4. using namespace std;
  5.  
  6. // Pointer to the JVM (Java Virtual Machine)
  7. JavaVM *jvm;
  8.  
  9. // Pointer to native interface
  10. JNIEnv *env;
  11.  
  12. //==================== prepare loading of Java VM ============================
  13.  
  14. // Initialization arguments
  15. JavaVMInitArgs vm_args;
  16.  
  17. // JVM invocation options
  18. JavaVMOption* options = new JavaVMOption[1];
  19.  
  20. // where to find java .class
  21. options[0].optionString = "-Djava.class.path=.;./libs/*.jar";
  22.  
  23. // minimum Java version
  24. vm_args.version = JNI_VERSION_1_6;
  25.  
  26. // number of options
  27. vm_args.nOptions = 1;
  28.  
  29. vm_args.options = options;
  30.  
  31. // invalid options make the JVM init fail
  32. vm_args.ignoreUnrecognized = false;
  33.  
  34. //================= load and initialize Java VM and JNI interface ===============
  35.  
  36. jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
  37. delete options;
  38.  
  39. if (rc != JNI_OK)
  40. {
  41.     if (rc == JNI_EVERSION)
  42.         cerr << "FATAL ERROR: JVM is outdated and doesn't meet requirements" << endl;
  43.     else if (rc == JNI_ENOMEM)
  44.         cerr << "FATAL ERROR: not enough memory for JVM" << endl;
  45.     else if (rc == JNI_EINVAL)
  46.         cerr << "FATAL ERROR: invalid argument for launching JVM" << endl;
  47.     else if (rc == JNI_EEXIST)
  48.         cerr << "FATAL ERROR: the process can only launch one JVM an not more" << endl;
  49.     else
  50.         cerr << "FATAL ERROR:  could not create the JVM instance (error code " << rc << ")" << endl;
  51.  
  52.     cin.get();
  53.     exit(EXIT_FAILURE);
  54. }
  55.  
  56. jint ver = env->GetVersion();
  57. cout << "JVM load succeeded. nVersion ";
  58. cout << ((ver >> 16) & 0x0f) << "." << (ver & 0x0f) << endl;
  59.  
  60. //========== Find Class ==========================
  61.  
  62. // Try to find the class
  63. jclass mClass = env->FindClass("MyClass");
  64. if (mClass == nullptr)
  65. {
  66.     cerr << "ERROR: class not found !";
  67.     exit(EXIT_FAILURE);
  68. }
  69.  
  70. cout << "Class MyTest found" << endl;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top