[英]run a executable jar from c++ code
我需要從一些C ++代碼內部執行一個jar
文件。
我嘗試了以下命令
int ret = execlp("java", "java", "-jar", "myprog.jar", (char *)0);
它工作正常。但是我的問題是我的c ++主線程在執行我的jar文件后停止了。我不想在執行jar文件后停止我的c ++主線程。我該怎么做。
問候
解決此問題的通常方法是先叉叉(),然后從新生成的進程中執行exec()。
您始終可以使用JNI啟動JVM並調用程序的main方法。
#include <jni.h> /* 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.