T
tony_lincoln
Yes they are. U r right. I should look a little bit more about the
manual. Sorry.
Tony
manual. Sorry.
Tony
sure you compile and link against the correct (and same) one.
I really thank Gordon Beaton and others a lot! Actually he showed me
already how to do it in his answers:
Just add one line in jni.h, to define JNI_VERSION as 1_4_2_08. Then I
can call java codes from C++ under Linux!
But still two quesions:
For the compiling and linking:
gcc -Wall -D_REENTRANT -I $JAVA_HOME/include -I
$JAVA_HOME/include/linux -c invoke.cpp
gcc -L $JAVA_HOME/jre/lib/i386/client invoke.o -lstdc++ -ljvm -lpthread
-o invoke
The parameters for gcc are really important. Without anyone of them,
the JNI calling can not work. But How do I know which parameter I
should choose?
Question two:
For the two libjvm.so,
/usr/lib/jvm/java-1.4.2-sun-1.4.2.05/jre/lib/i386/client/libjvm.so
/usr/lib/jvm/java-1.4.2-sun-1.4.2.05/jre/lib/i386/server/libjvm.so
I was told by Gordon that I should set the ID_LIBRARY_PATH like this:
/usr/lib/jvm/java-1.4.2-sun-1.4.2_05/jre/lib/i386/server
/usr/lib/jvm/java-1.4.2-sun-1.4.2_05/jre/lib/i386
But why?
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.
No, u did not suggest that I should add my own JNI_VERSION to the
header file. It was my idea. But this idea was initiated by your and
other's words. Thanks a lot.
Tony
If I put the same parameter list in do_it() as in main(), that is,
String[] args, it can work.
But if the argument list in do_it() is not same as in main(), what is
the correct code?
Unexpected Signal : 11 occurred at PC=0x402B420F
But in my library headers there is only:
#define JNI_VERSION_1_1 0x00010001
#define JNI_VERSION_1_2 0x00010002
There is no definition for JNI_VERSION_1_4 at all. As my current java
version is 1.4, I guess that I should add this new definition into the
header.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.