call java from c++: where to put class

G

Gordon Beaton

I do not think that there is errors in my source code.

I don't agree.
I made a test and the source code is following:

I was referring to your C++ code, not the Java code. I thought that
was clear from the example in my earlier reply.

/gordon
 
T

tony_lincoln

I think that you are correct, now I guess #that this error is caused by
CallStaticVoidMethod() in C++ codes. What I have done to prove this is:
I simplied the C++ codes and Java codes, as following:

/*for C++,debugged with Visual C++ 6.0*/
// invoke.cpp

#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");
if (cls == 0) printf("Sorry, I can't find the class");

jmethodID get_main_id;

if(cls != NULL)
{
get_main_id =
env->GetStaticMethodID(cls,"main","([Ljava/lang/String;)V");

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);
fprintf(stdout, "This is invokeSimplified7.\n");

}// end IF.

}// end IF.


jvm->DestroyJavaVM();
fprintf(stdout, "Java VM destory\n");
}//end main.

**************************************************************************************************************

In the main() of Java codes DemoMain.java, there are only 2 sentences:
System.out.println ("This is a test");
IOTest.print2Lines();

IOTest is another java class located in a different directory. There
are only 2 sentences in print2Lines():
System.out.println ("Line1");
System.out.println ("Line2");

I simply these classes, so no parameter was transmitted in different
methods or classes.
I run the c++ codes, the output is like this:
________________________________________________________________________________
....
This is invokeSimplified6.
[Dynamic-linking native method java.io.FileOutputStream.writeBytes ...
JNI]
This is a test.
This is invokeSimplified7.
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# Internal Error (455843455054494F4E530E43505000FF), pid=3704,
tid=4064
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_02-b09 interpreted mode,
sharing)
# An error report file with more information is saved as
hs_err_pid3704.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
---------------------------------------------------------------------------------------------------------------------------------

So I know that this error is caused by CallStaticVoidMethod(). But how
to change it so the output is correct?
Thanks a lot.
tony
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top