0

I have the below function 'initializeAlgo' that is used to initialize the Java VM with a specific Algo jar file and later I use the java environment created to call other fns in the Jar.

JavaVM *jvm;
JNIEnv *env;

void initializeAlgo(const std::string& algoSourceJar) {    
    JavaVMInitArgs vm_args;                                                     
    JavaVMOption* options = new JavaVMOption[1];
    std::string classPath = "-Djava.class.path=" + algoSourceJar;    
    options[0].optionString = new char[classPath.length()]();
    strcpy(options[0].optionString, classPath.c_str());
    vm_args.version = JNI_VERSION_1_6;                                          
    vm_args.nOptions = 1;                                                       
    vm_args.options = options;
    vm_args.ignoreUnrecognized = false;

   //================= load and initialize Java VM and JNI interface============

  initStatus = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
}

Suppose If i have to support dynamic loading of multiple Algo sources that satisfy a certain interface. i.e. load Algo_1 and do some process and record results, and without closing the application I want to load Algo_2 and do the same process and record results. And they both have the same namespace and fns. Is it recommended to do this in the same instance of the application?

Hari
  • 21
  • 3
  • Possible duplicate of [Why have one JVM per application?](https://stackoverflow.com/questions/13539132/why-have-one-jvm-per-application) – Andrew Henle Aug 03 '17 at 17:26
  • Only one JVM instance can be run in a process. – Andrew Henle Aug 03 '17 at 17:27
  • Can we retain the JVM instance and just update the class path to point to a new location(algo_2.jar) and remove the last included location(algo_1.jar) – Hari Aug 04 '17 at 00:30
  • You can't "just update" the classpath. But you can create class loaders with the path(s) you need. – user2543253 Aug 04 '17 at 11:34
  • If algorithms satisfy the same interface, simply create abstract class and then, from your C++ code call JVM via JNI. This way, you will not be forced to create multiple JVMs. You can take a look here for a sample how to call one JVM from multiple threads: http://jnicookbook.owsiak.org/recipe-no-027/ – Oo.oO Aug 05 '17 at 12:00

0 Answers0