E
Edsoncv
Hello
I'm a newbie Java programmer and I'm facing problems with a JNI
interface that calls Java from a C++ code. The code simply calls Java
to evaluate a matemathical function. The code runs until the line 40 ("
xj = env->NewDoubleArray(4);" ) and exits with the exception "Out of
memory", which means that it couldn't allocate the DoubleArray. I hope
someone could help me. The code is below :
C++ code:
Bool eval_f(Index n, Number* x, Bool new_x,
Number* obj_value, UserDataPtr user_data)
{
assert(n == 4);
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[5];
jint res;
jclass cls;
jmethodID mid;
jdoubleArray xj;
double res_obj;
options[0].optionString = "-Xms4M";
options[1].optionString = "-Xmx64M";
options[2].optionString = "-Xss512K";
options[3].optionString = "-Xoss400K";
options[4].optionString = "-Djava.class.path=.";
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 5;
vm_args.ignoreUnrecognized = JNI_FALSE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
if (res < 0) {
cerr<< "Can't create Java VM" << endl;
goto destroy;
}
cls = env->FindClass("res_fobj");
if (cls == 0) {
cerr << "Can't find res_fobj class" << endl;
goto destroy;
}
mid = env->GetStaticMethodID(cls, "main", "([D)D");
if (mid == 0) {
cerr<< "Can't find res_fobj.main" << endl;
goto destroy;
}
xj = env->NewDoubleArray(4);
if (xj == 0) {
cerr << "Out of memory" << endl;
goto destroy;
}
res_obj = env->CallStaticDoubleMethod(cls, mid, xj);
if (res_obj == 0) {
cerr << "Out of memory" << endl;
goto destroy;
}
cout << "resobj Calculado por java:\t" << res_obj << endl;
*obj_value = res_obj;
cout << "obj_value atribuído: \t" << *obj_value << endl;
return TRUE;
destroy:
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}
jvm->DestroyJavaVM();
return FALSE;
}
Java code:
public class res_fobj {
public static double main(double x[]){
double res;
res = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2];
return res;
}
}
Thanks
I'm a newbie Java programmer and I'm facing problems with a JNI
interface that calls Java from a C++ code. The code simply calls Java
to evaluate a matemathical function. The code runs until the line 40 ("
xj = env->NewDoubleArray(4);" ) and exits with the exception "Out of
memory", which means that it couldn't allocate the DoubleArray. I hope
someone could help me. The code is below :
C++ code:
Bool eval_f(Index n, Number* x, Bool new_x,
Number* obj_value, UserDataPtr user_data)
{
assert(n == 4);
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[5];
jint res;
jclass cls;
jmethodID mid;
jdoubleArray xj;
double res_obj;
options[0].optionString = "-Xms4M";
options[1].optionString = "-Xmx64M";
options[2].optionString = "-Xss512K";
options[3].optionString = "-Xoss400K";
options[4].optionString = "-Djava.class.path=.";
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 5;
vm_args.ignoreUnrecognized = JNI_FALSE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
if (res < 0) {
cerr<< "Can't create Java VM" << endl;
goto destroy;
}
cls = env->FindClass("res_fobj");
if (cls == 0) {
cerr << "Can't find res_fobj class" << endl;
goto destroy;
}
mid = env->GetStaticMethodID(cls, "main", "([D)D");
if (mid == 0) {
cerr<< "Can't find res_fobj.main" << endl;
goto destroy;
}
xj = env->NewDoubleArray(4);
if (xj == 0) {
cerr << "Out of memory" << endl;
goto destroy;
}
res_obj = env->CallStaticDoubleMethod(cls, mid, xj);
if (res_obj == 0) {
cerr << "Out of memory" << endl;
goto destroy;
}
cout << "resobj Calculado por java:\t" << res_obj << endl;
*obj_value = res_obj;
cout << "obj_value atribuído: \t" << *obj_value << endl;
return TRUE;
destroy:
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}
jvm->DestroyJavaVM();
return FALSE;
}
Java code:
public class res_fobj {
public static double main(double x[]){
double res;
res = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2];
return res;
}
}
Thanks