Use JAR file in C ++ / c

Possible duplicate:
How to access a Java method in a C ++ application

I need to use a JAR file in a C ++ program. that is, from C ++ I need to call the java function, for example, In java there is a function that takes 2 integers and returns their addition. Now I need to call this function from C ++. Please help me Thanks in advance.

+3
source share
1 answer

You need to use the Java Invocation API described here. This sample code (from this link) shows how to load Machine into a virtual Java environment and use it to invoke a static Java method called test with the int argument, which is in the Main class. In this example, the path to the JAR file will be set using the vm_args.classpath variable.

 #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(); 

If you want to call a non-static method, the code will be slightly different, and the rest of the Java Native Interface tutorial explains everything you need to know.

+10
source

Source: https://habr.com/ru/post/1203863/


All Articles