1

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

i tried this following command

int ret = execlp("java", "java", "-jar", "myprog.jar", (char *)0);

it's working fine.but my problem is my c++ main thread was stopped after executing my jar file.i don't want to stop my c++ main thread after excecuting jar file.how can i do this.

Regards

Rams
  • 2,141
  • 5
  • 33
  • 59

2 Answers2

4

The normal way to solve this problem is to fork() and then exec() from the newly spawned process.

ObscureRobot
  • 7,306
  • 2
  • 27
  • 36
3

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

 #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();
dekz
  • 805
  • 1
  • 8
  • 17
  • 3
    I wish someone would explain this further... I keep seeing this answer to other questions, but the "..." part is still "..."... I don't know what to put there... – Katie Mar 21 '13 at 21:42
  • Link to further resources is broken – gkhaos Nov 15 '21 at 08:45