argument transfering in JNI, problem in calling Java GUI

T

tony_lincoln

Dear friends,

I am using JNI to call java from C++, in jdk1.4.1 and Visual C++ 6. It
works well, but without argument transfering:
(I posted this message here, it is not the same as the 3 messages I
posted before. If you read carefully about these 4 messages, you will
find that I am making progress, although slowly. I wish people will not
mind my questions)

Question 1 is about the argument transfering:

In the C++ code, I call the main() of java codes:

jclass string = env->FindClass("java/lang/String");
jobjectArray args = env->NewObjectArray(0,string, NULL);
env->CallStaticVoidMethod(cls, get_main_id, args);

The java code is like this:
String[] fileNamesList =
LM_SearchingFiles.searchGivenFilesInDir(args[0], args[1]);
// if I set args[0] = "E:", and args[1] = "txt", class
LM_SearchingFiles is to search all txt files in disk E.

When I execute the C++ code, there is one error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

I am sure there are sth wrong in one sentence of C++ codes:
env->CallStaticVoidMethod(cls, get_main_id, args);

But where?

+++++++++++++++++++++++++++++++++++Question
2+++++++++++++++++++++++++++++++++++

Question 2 is about GUI made by java:
I made one GUI in Java, and would like to call this Java code from its
main(). In its main() :
public static void main(String[] args)
throws IOException{
frame = new MainFrame(new Menu_3D(), 400, 200);
frame.setLayout(new BorderLayout());
MenuBar bar = new MenuBar();
frame.setMenuBar(bar);
}// end main().
But when I call this java class from C++, the error is :

**************************************************************************************************************************
Sorry, I can't find the class
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# Internal Error (455843455054494F4E530E43505000FF), pid=3888,
tid=3976
#
# 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_pid3888.log
***************************************************************************************************************************

When this kind of GUI classes can not work?
I tried as well to call some of my java3D codes, but the java3D classes
still can not be found(the simplest one like HelloUniverse.class). The
error message is like the above. Why?
Thanks. tony
 
G

Gordon Beaton

In the C++ code, I call the main() of java codes:

jclass string = env->FindClass("java/lang/String");
jobjectArray args = env->NewObjectArray(0,string, NULL);

You have created an array of length 0 that you pass to your main()
java method. An array of length 0 has no elements, so the following
line fails:
String[] fileNamesList =
LM_SearchingFiles.searchGivenFilesInDir(args[0], args[1]);


I believe the other error is due to you failing to catch an exception.

Several times now I have several posted code similar to this in
response to your questions:

if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
env->ExceptionClear()
}

Use this *immediately* after calling CallStaticVoidMethoid(main). If
an exception occurs in java main(), subsequent calls into the JVM may
cause it to crash.

/gordon
 
C

Chris Uppal

Gordon said:
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
env->ExceptionClear()
}

Use this *immediately* after calling CallStaticVoidMethoid(main). If
an exception occurs in java main(), subsequent calls into the JVM may
cause it to crash.

Or, if the OP is lucky enough to avoid a crash, cause all subsequent JNI calls
to fail inexplicably.

-- chris
 
T

tony_lincoln

Thanks a lot. Thanks for the wonderful JNI, now my whole Java codes can
be invoked in C++. Perfect.
Actually if I put all java classes into the correct dir and was careful
enough, it should work.
Until now I would like to say, JNI is a nice tool.
Thank you all for the nice help!
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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top