JNI_CreateJavaVM doesn't work for GUI

Discussion in 'Java' started by James Cassidy, Jun 19, 2004.

  1. Can anyone please lend some assistance? I've scanned the newsgroup archives as well as the net,
    but not been able to get this to work.

    My code to load a jvm works from a console program, but returns -1 when used in an MFC GUI program.
    The vfprintf even works in the console version, but not in the MFC version!??




    static jint JNICALL my_vfprintf(FILE *fp, const char *format, va_list args)
    {
    char buf[1024];
    _vsnprintf(buf, sizeof(buf), format, args);
    return 1;
    }


    void CTestApp::loadJvm( void )
    {
    JavaVMInitArgs vm_args;
    JavaVMOption options[5];
    JavaVM *jvm;
    JNIEnv *env;
    jint res;

    options[0].optionString = "-Djava.compiler=NONE";
    options[1].optionString = "-Djava.class.path=c:\\j2sdk1.4.2_04\\jre\\lib\\rt.jar.";
    options[2].optionString = "-Djava.library.path=c:\\j2sdk1.4.2_04\\bin";
    options[3].optionString = "-verbose:jni";
    options[4].optionString = "vfprintf";
    options[4].extraInfo = my_vfprintf;

    vm_args.version = JNI_VERSION_1_4;
    vm_args.options = options;
    vm_args.nOptions = 5;
    vm_args.ignoreUnrecognized = JNI_TRUE;

    res = JNI_CreateJavaVM( &jvm, (void **) &env, &vm_args );

    if( res < 0 ) {
    MessageBox( NULL, "JNI_CreateJavaVM failed.", "Error loading Java VM", MB_OK );
    }

    }
     
    James Cassidy, Jun 19, 2004
    #1
    1. Advertising

  2. James Cassidy

    Jim Sculley Guest

    James Cassidy wrote:
    > Can anyone please lend some assistance? I've scanned the newsgroup archives as well as the net,
    > but not been able to get this to work.
    >
    > My code to load a jvm works from a console program, but returns -1 when used in an MFC GUI program.
    > The vfprintf even works in the console version, but not in the MFC version!??


    The jvm shared library (jvm.dll) expects to be in a particular location
    relative to the runtime libraries. Did you make any changes from your
    console program to your MFC program that might have impacted this?

    Jim S.




    --
    Remove my extraneous mandibular appendages to reply via email.
     
    Jim Sculley, Jun 19, 2004
    #2
    1. Advertising

  3. On Sat, 19 Jun 2004 09:58:59 -0400, Jim Sculley <> wrote:

    >James Cassidy wrote:
    >> Can anyone please lend some assistance? I've scanned the newsgroup archives as well as the net,
    >> but not been able to get this to work.
    >>
    >> My code to load a jvm works from a console program, but returns -1 when used in an MFC GUI program.
    >> The vfprintf even works in the console version, but not in the MFC version!??

    >
    >The jvm shared library (jvm.dll) expects to be in a particular location
    >relative to the runtime libraries. Did you make any changes from your
    >console program to your MFC program that might have impacted this?
    >
    >Jim S.


    Thanks for the reply Jim.

    No changes. I've installed J2SDK1.4.2_04 in the location c:\j2sdk1.4.2_04. The jvm.dll is in the
    location:

    c:\j2sdk1.4.2_04\jre\bin\server\jvm.dll

    I've rechecked the code many times (there still might be a mistake), but it appears to be the same
    as in the examples. I'm wondering, as you suggest, that some dll, lib or jar isn't being found by
    the mfc version that is being found by the console version. But I have no idea which.

    My project settings link tab has the 'c:\j2sdk1.4.2_04\lib\jvm.lib' library specified.

    My dos path is set to:

    .....;c:\j2sdk1.4.2_04\jre\bin\server;c:\j2sdk1.4.2_04\lib;c:\j2sdk1.4.2_04\bin

    I have no dos CLASSPATH set.

    Does any of this info show where I'm shooting myself in the foot (repeatedly)? :)

    Regards,
    Jim.
     
    James Cassidy, Jun 19, 2004
    #3
  4. Hava a look into the src.zip file of your JDK installation.
    In this ZIP file you will find the directory launcher, containing some
    native files. This files will show you how to create a JVM within a
    native C/C++ program.

    Best,
    Manfred
     
    Manfred Rosenboom, Jun 21, 2004
    #4
  5. James Cassidy

    Chris Uppal Guest

    James Cassidy wrote:

    > Does any of this info show where I'm shooting myself in the foot
    > (repeatedly)? :)


    You might be able to get some insight by using tools like Dependency Walker
    <http://www.dependencywalker.com/>
    or FileMon
    <http://www.sysinternals.com/ntw2k/source/filemon.shtml>

    -- chris
     
    Chris Uppal, Jun 21, 2004
    #5
  6. Thanks Chris! That did the trick. It gave enough clues for me to figure out that there was a
    jvm.dll sitting in my executable directory. It must have been using this one rather than the one
    in the JDK installation directory. This was messing everything up as Jim suggested in his reply.

    What's interesting is that this type of mistake doesn't produce any clues as to what's happening.

    Once again, thanks to everyone for their help!

    Regards,
    Jim.

    On Mon, 21 Jun 2004 09:54:53 +0100, "Chris Uppal" <-THIS.org> wrote:

    >James Cassidy wrote:
    >
    >> Does any of this info show where I'm shooting myself in the foot
    >> (repeatedly)? :)

    >
    >You might be able to get some insight by using tools like Dependency Walker
    > <http://www.dependencywalker.com/>
    >or FileMon
    > <http://www.sysinternals.com/ntw2k/source/filemon.shtml>
    >
    > -- chris
    >
     
    James Cassidy, Jun 21, 2004
    #6
  7. James Cassidy

    Roedy Green Guest

    On Mon, 21 Jun 2004 09:17:25 -0400, James Cassidy <> wrote
    or quoted :

    >Thanks Chris! That did the trick. It gave enough clues for me to figure out that there was a
    >jvm.dll sitting in my executable directory. It must have been using this one rather than the one
    >in the JDK installation directory. This was messing everything up as Jim suggested in his reply.


    Somebody please implement this student project to help deliver us from
    such evil.

    see http://mindprod.com/projects/pathtool.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 21, 2004
    #7
    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. Chao Dong
    Replies:
    2
    Views:
    6,471
    subhanshoo
    Apr 16, 2008
  2. Steve
    Replies:
    2
    Views:
    2,853
    Steve
    Dec 22, 2003
  3. kevin
    Replies:
    3
    Views:
    2,462
    perry
    May 15, 2004
  4. buzz
    Replies:
    0
    Views:
    3,079
  5. Replies:
    0
    Views:
    524
Loading...

Share This Page