Reputation: 14309
Using JNI I'm trying to create an instance of a Java class from a C++ project but it fails. I have tested the same code with a simple Java class and it works. The difference is that my actual classpath is a directory containing a bunch of jars. The class I need instantiating CmaesClient
belongs to one of those jars.
This is the C++ code:
JavaVM* jvm = NULL;
JNIEnv *env = NULL;
JavaVMInitArgs vm_args;
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=/home/azg/code/hpcmom/target/1.1.9-SNAPSHOT/hpcmom-cmaes";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args);
if (jvm == NULL) {
std::cout << "Failed creating JVM" << std::endl;
} else {
std::cout << "Succeeded creating JVM" << std::endl;
}
jclass clazz = env->FindClass("com.sfoam.hpcmom.cmaes.CmaesClient");
if (clazz == NULL) {
std::cout << "Failed creating CmaesClient" << std::endl;
} else {
std::cout << "Succeeded creating CmaesClient" << std::endl;
}
jmethodID constr = env->GetMethodID(clazz, "<init>", "([Ljava/lang/String;)V");
jstring jarPath = env->NewStringUTF("/home/azg/code/hpcmom/target/1.1.9-SNAPSHOT/hpcmom-cmaes/hpcmom-cmaes-1.1.9-SNAPSHOT.jar");
jobject object = env->NewObject(clazz, constr, jarPath);
delete options;
jvm->DestroyJavaVM();
and the error meaning it could not find the class, therefore seems something wrong loading the classes or?
Succeeded creating JVM
Failed creating CmaesClient
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f5601067214, pid=25496, tid=140007371831104
#
# JRE version: Java(TM) SE Runtime Environment (7.0_51-b13) (build 1.7.0_51-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.51-b03 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V [libjvm.so+0x645214] get_method_id(JNIEnv_*, _jclass*, char const*, char const*, bool, Thread*)+0x84
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/azg/code/sfoml/debug/hs_err_pid25496.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
Aborted (core dumped)
The directory /home/azg/code/hpcmom/target/1.1.9-SNAPSHOT/hpcmom-cmaes
contains all the needed jars.
Upvotes: 1
Views: 1363
Reputation: 121
name
: a fully-qualified class name (that is, a package name, delimited by “/
”, followed by the class name).
Change
env->FindClass("com.sfoam.hpcmom.cmaes.CmaesClient");
to
env->FindClass("com/sfoam/hpcmom/cmaes/CmaesClient");
(I.e. replace each .
with /
).
Upvotes: 4