I have a jar file named Untitled.jar that prints some text message. It has a void function call giveMeString() and the class name is xegerImplementation.
giveMeString() generates string for the regular expression defined inside the giveMeString(). I needed to generate strings given a regular expression in c++. Since java has a library called xeger that does this, so I thought to create a jar and then import that inside the c++ code.
I have implemented the following code which I got from http://ift.tt/2A9gbku and tweaked it to match my jar file.
The cpp file name is main.cpp and has the following code.
#include <iostream>
#include "jni.h"
int main(){
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 9 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=/home/aaa/Desktop/Untitled.jar";
vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
delete options;
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("xegerImplementation");
jmethodID mid = env->GetStaticMethodID(cls, "giveMeString", "(I)V");
env->CallStaticVoidMethod(cls, mid);
/* We are done. */
jvm->DestroyJavaVM();
}
When I was using g++ main.cpp -o main to generate the executable main file. But it gave me the following error :
/tmp/ccf3LtdA.o: In function `main':
main.cpp:(.text+0x5d): undefined reference to `JNI_CreateJavaVM'
collect2: error: ld returned 1 exit status
Then with the help from stackoverflow, I was able to solve this error by compiling it with the following code:
g++ -g -I/usr/lib/jvm/java-8-oracle/include/ -I/usr/lib/jvm/java-8-oracle/include/linux/ -L/usr/bin/java -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/ main.cpp -o main -ljvm
It generates the executable main file.
Then, I tried to run the executable main file with the following command
./main
It gives me the following error:
./main: error while loading shared libraries: libjvm.so: cannot open shared object file: No such file or directory
I tried to add the path to libjvm.so to the LD_LIBRARY_PATH. But still it doesnot work.
What am I doing wrong? Any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire