package/path setting: Call Java from C++

T

tony_lincoln

Dear friends,

I am using JNI to call java from C++. I use j2sdk1.4.1 and Visual C++
6. It works well if all codes are in the same directory. That is,
invoke.cpp, DemoMain.class and IOTestOhnePackage.class are in the same
dir, invoke.cpp call the main() in DemoMain.class, and DemoMain.class
uses one method in IOTestOhnePackage.class.

However, for the complicated system, DemoMain.class should be able to
use lots of java classes which are distributed in different
directories. In this case, Package is necessary.

If I add "Package com.tony.JNIClass;" into the 1st line of
IOTestOhnePackage.java. Compile it, then put this class into
E:\codes_Tony\com\tony\JNIClass, at the same time I set Parameters in
Visual C++ 6:

In Tools->Options->Include Files: E:\codes_Tony
In Tools->Options->Library Files: E:\codes_Tony

When I execute invoke.cpp, the program did not crash, but the part to
call class IOTestOhnePackage was jumped.
I know this is because of some setting problems, maybe in Visual C++,
maybe in System Path. But how? Any hints?
Thanks a lot.
Tony
 
A

Andrew Thompson

I am using JNI to call java from C++.

Is that the same Java/JNI & C++ you were mentioning
<http://groups.google.com.au/group/c..._frm/thread/e81fe3ec5a45b187/bf07c2bcaa9b0ab3>
...and, here
<http://groups.google.com.au/group/c..._frm/thread/7a40b34d6496e68e/3290257d3e9eb5fe>
?

A couple of notes, before I retire from this thread..
- There are very few people posting to this forum with the
expertise to resolve your technical problem.
- There are very few people on this forum who would have
the patience to deal with a poster who is as frustratingly
vague and contradictroy as you seem to be.
- The *only* person (that I know of) on this forum, that combines
the above two qualities, has asked you once already to keep
the subject line constant.

I am not entirely sure why you felt this thread needed a
new subject line, perhaps my own assessment was wrong.

I wish you success in resolving your technical problem.
 
C

Chris Uppal

However, for the complicated system, DemoMain.class should be able to
use lots of java classes which are distributed in different
directories. In this case, Package is necessary.

Do you know how to use the -classpath option to the JAVA.EXE program to tell it
where to find class files from different packages ? If not then you should
find out before going on; it's basic stuff and you /have/ to understand it
before you can make sensible use of "advanced" features like JNI.

Once you do know how to do it, it's simple. You just pass the same
-classpath xxx;yyy;zzzz
arguments to the Java VM as two of the strings in the array of options in the
initArgs passed to JNI_CreateJavaVM().

In Tools->Options->Include Files: E:\codes_Tony
In Tools->Options->Library Files: E:\codes_Tony

Neither of these options has anything at all to do with where the JVM will look
for classfiles, they are used by the VC6 IDE to find stuff to compile, /not/ by
your running program.

-- chris
 
T

tony_lincoln

OK, it works now. It's nothing to do with classpath. Just create dir
com/tony/jniClasses in the same dir as invoke.cpp, copy the
IOTest.class which includes the sentence "Package com.tony.JNIClass"
into this folder, then it works. As the VC6 IDE find this class from
the current dir, so the com/tony/jniClasses should be created in this
dir.
But Chris is right in some way:
In Tools->Options->Include Files: E:\codes_Tony
In Tools->Options->Library Files: E:\codes_Tony
Neither of these options has anything at all to do with where the JVM
will look
for classfiles

This is true. Thanks a lot.
At the same time, forget the rude words from Andrew Thompson. It seems
that he was not well educated.
Thank you. tony
 
G

Gordon Beaton

OK, it works now. It's nothing to do with classpath. Just create dir
com/tony/jniClasses in the same dir as invoke.cpp, copy the
IOTest.class which includes the sentence "Package com.tony.JNIClass"
into this folder, then it works.

The following code after CallStaticVoidMethod() would have told you
that the class could not be found:

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

/gordon
 
T

tony_lincoln

Thanks for your code. I added

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

at the end of CallStaticVoidMethod(), like the following:

....
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");

if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}
fprintf(stdout, "This is invokeSimplified8.\n");
}// end IF.

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

The output is:

"This is invokeSimplified6.
[Dynamic-linking native method java.io.FileOutputStream.writeBytes ...
JNI]
This is test01.
double d = 300.0
This is invokeSimplified7.
This is invokeSimplified8.
Java VM destory"

Between "This is invokeSimplified7" and "This is invokeSimplified8",
nothing happens...
so what is the aim of your code

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

?

THanks . 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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top