run a executable jar from c++ code

前端 未结 2 1981
后悔当初
后悔当初 2021-01-25 04:17

I need to execute a jar file from inside of some C++ code.

i tried this following command

int ret = execlp(\"java\", \"java\", \"-jar\", \"m         


        
2条回答
  •  无人及你
    2021-01-25 05:01

    You can always use JNI to launch a JVM and call the main method of the program.

     #include        /* where everything is defined */
        ...
        JavaVM *jvm;       /* denotes a Java VM */
        JNIEnv *env;       /* pointer to native method interface */
        JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization arguments */
        vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
        /* Get the default initialization arguments and set the class 
         * path */
        JNI_GetDefaultJavaVMInitArgs(&vm_args);
        vm_args.classpath = ...;
        /* load and initialize a Java VM, return a JNI interface 
         * pointer in env */
        JNI_CreateJavaVM(&jvm, &env, &vm_args);
        /* invoke the Main.test method using the JNI */
        jclass cls = env->FindClass("Main");
        jmethodID mid = env->GetStaticMethodID(cls, "test", "(I)V");
        env->CallStaticVoidMethod(cls, mid, 100);
        /* We are done. */
        jvm->DestroyJavaVM();
    

提交回复
热议问题