T
tony_lincoln
Dear Friends,
Under Linux, I tried to call java from C++ using JNI. The following is
the C++ code which calls one java class named Menu_3D.
When I compiled it using g++, there were many mistakes like:
........
invoke.cpp:26: error: 'struct JavaVMInitArgs' has no member named
'version'
invoke.cpp:26: error: `JNI_VERSION_1_4' undeclared (first use this
function)
invoke.cpp:27: error: 'struct JavaVMInitArgs' has no member named
'nOptions'
invoke.cpp:31: error: `jint' undeclared (first use this function)
invoke.cpp:31: error: syntax error before `=' token
invoke.cpp:32: error: `res' undeclared (first use this function)
invoke.cpp:41: error: `FindClass' undeclared (first use this function)
invoke.cpp:50: error: `GetStaticMethodID' undeclared (first use this
function)
invoke.cpp:56: error: `NewObjectArray' undeclared (first use this
function)
invoke.cpp:60: error: `CallStaticVoidMethod' undeclared (first use this
function)
invoke.cpp:73: error: `DestroyJavaVM' undeclared (first use this
function)
........
The above errors are only one part of the whole errors. I know that I
am wrong in this line:
"#pragma comment
(lib,"E:\\Programme\\Java\\jdk1.5.0_02\\lib\\jvm.lib")"
But how can I find the jvm.lib? I tried and I can not. Does invoke.cpp
need jvm.lib under Linux?
Or are there other reasons to cause the errors?
Thanks a lot
tony
__________________________________________________________________________
/*for C++,debugged with Visual C++ 6.0*/
#ifndef __cplusplus
#define __cplusplus
#endif
#include "jni.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment (lib,"E:\\Programme\\Java\\jdk1.5.0_02\\lib\\jvm.lib")
void main() {
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.classpath=.";
options[2].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
};
// jclass cls = env->FindClass("DemoMain");
// This sentence can work. Just put it into the same directory as
invoke.cpp.
jclass cls = env->FindClass("Menu_3D");
if (cls == 0) printf("Sorry, I can't find the class");
fprintf(stdout, "This is invokeSimplified4.\n");
jmethodID get_main_id;
if(cls != NULL)
{
get_main_id =
env->GetStaticMethodID(cls,"main","([Ljava/lang/StringV");
fprintf(stdout, "This is invokeSimplified5.\n");
if(get_main_id != NULL )
{
jclass string = env->FindClass("java/lang/String");
jobjectArray args = env->NewObjectArray(0,string, NULL);
fprintf(stdout, "This is invokeSimplified6.\n");
env->CallStaticVoidMethod(cls, get_main_id, args);
/*
if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
env->ExceptionClear();
}
*/ fprintf(stdout, "This is invokeSimplified7.\n");
}// end IF.
}// end IF.
jvm->DestroyJavaVM();
fprintf(stdout, "Java VM destory\n");
}//end main.
______________________________________________________codes end.
Under Linux, I tried to call java from C++ using JNI. The following is
the C++ code which calls one java class named Menu_3D.
When I compiled it using g++, there were many mistakes like:
........
invoke.cpp:26: error: 'struct JavaVMInitArgs' has no member named
'version'
invoke.cpp:26: error: `JNI_VERSION_1_4' undeclared (first use this
function)
invoke.cpp:27: error: 'struct JavaVMInitArgs' has no member named
'nOptions'
invoke.cpp:31: error: `jint' undeclared (first use this function)
invoke.cpp:31: error: syntax error before `=' token
invoke.cpp:32: error: `res' undeclared (first use this function)
invoke.cpp:41: error: `FindClass' undeclared (first use this function)
invoke.cpp:50: error: `GetStaticMethodID' undeclared (first use this
function)
invoke.cpp:56: error: `NewObjectArray' undeclared (first use this
function)
invoke.cpp:60: error: `CallStaticVoidMethod' undeclared (first use this
function)
invoke.cpp:73: error: `DestroyJavaVM' undeclared (first use this
function)
........
The above errors are only one part of the whole errors. I know that I
am wrong in this line:
"#pragma comment
(lib,"E:\\Programme\\Java\\jdk1.5.0_02\\lib\\jvm.lib")"
But how can I find the jvm.lib? I tried and I can not. Does invoke.cpp
need jvm.lib under Linux?
Or are there other reasons to cause the errors?
Thanks a lot
tony
__________________________________________________________________________
/*for C++,debugged with Visual C++ 6.0*/
#ifndef __cplusplus
#define __cplusplus
#endif
#include "jni.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment (lib,"E:\\Programme\\Java\\jdk1.5.0_02\\lib\\jvm.lib")
void main() {
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.classpath=.";
options[2].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
};
// jclass cls = env->FindClass("DemoMain");
// This sentence can work. Just put it into the same directory as
invoke.cpp.
jclass cls = env->FindClass("Menu_3D");
if (cls == 0) printf("Sorry, I can't find the class");
fprintf(stdout, "This is invokeSimplified4.\n");
jmethodID get_main_id;
if(cls != NULL)
{
get_main_id =
env->GetStaticMethodID(cls,"main","([Ljava/lang/StringV");
fprintf(stdout, "This is invokeSimplified5.\n");
if(get_main_id != NULL )
{
jclass string = env->FindClass("java/lang/String");
jobjectArray args = env->NewObjectArray(0,string, NULL);
fprintf(stdout, "This is invokeSimplified6.\n");
env->CallStaticVoidMethod(cls, get_main_id, args);
/*
if (env->ExceptionOccurred())
{
env->ExceptionDescribe();
env->ExceptionClear();
}
*/ fprintf(stdout, "This is invokeSimplified7.\n");
}// end IF.
}// end IF.
jvm->DestroyJavaVM();
fprintf(stdout, "Java VM destory\n");
}//end main.
______________________________________________________codes end.