package/path setting: Call Java from C++

Discussion in 'Java' started by tony_lincoln@yahoo.com, Aug 18, 2005.

  1. Guest

    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
    , Aug 18, 2005
    #1
    1. Advertising

  2. On 18 Aug 2005 03:37:40 -0700, wrote:

    > 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/comp.lang.java.programmer/browse_frm/thread/e81fe3ec5a45b187/bf07c2bcaa9b0ab3>
    ...and, here
    <http://groups.google.com.au/group/comp.lang.java.programmer/browse_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.

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "Any minute now, I'm expectin' all hell to break loose.."
    Bob Dylan 'Things Have Changed'
    Andrew Thompson, Aug 18, 2005
    #2
    1. Advertising

  3. Chris Uppal Guest

    wrote:

    > 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
    Chris Uppal, Aug 18, 2005
    #3
  4. Guest

    This sounds reasonable. I will try it. Thanks a lot.
    tony
    , Aug 18, 2005
    #4
  5. Guest

    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
    , Aug 18, 2005
    #5
  6. On 18 Aug 2005 06:33:16 -0700, wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Aug 18, 2005
    #6
  7. Guest

    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
    , Aug 19, 2005
    #7
  8. Roedy Green Guest

    On 18 Aug 2005 03:37:40 -0700, wrote or quoted
    :

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


    Put all your C++ dlls you load/call from Java into a jar with no
    package name member names.




    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com
    Roedy Green, Aug 23, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parvinder
    Replies:
    6
    Views:
    735
    Thomas G. Marshall
    Feb 27, 2005
  2. Aaron Fude
    Replies:
    1
    Views:
    19,622
    Gordon Beaton
    Sep 16, 2004
  3. wylbur37
    Replies:
    2
    Views:
    415
    John McGrath
    Mar 25, 2005
  4. Replies:
    3
    Views:
    6,327
  5. Dave
    Replies:
    2
    Views:
    461
Loading...

Share This Page