Error invoking JVM through JNI

Discussion in 'Java' started by Bruce, Feb 13, 2006.

  1. Bruce

    Bruce Guest

    Hi all,

    I have a problem invoking java VM by c code through JNI. The jdk is
    j2sdk1.4.2_07. I use gcc in MinGW on win XP os.

    I can compile and link the program correctly, but when I run the result
    ..exe file, it reported that the JNI_CreateJavaVM had failed.

    The InvokeJVM code is as follows:
    -----------------------------------------------------------------
    #include <jni.h>

    typedef pxdbJVM pxdbJVM;
    struct pxdbJVM
    {
    JavaVM *jvm;
    JNIEnv *jenv;
    };

    struct pxdbJVM pxdbJvm;

    int InvokeJVM( pxdbJVM *jvm )
    {
    jint res = 0;

    #ifdef JNI_VERSION_1_2
    JavaVMInitArgs vm_args;
    JavaVMOption options[2];
    options[0].optionString = "-Djava.class.path=.";
    options[1].optionString = "-Xmx64m";

    memset( &vm_args, 0, sizeof( vm_args ));
    vm_args.version = JNI_VERSION_1_2;
    vm_args.options = options;
    vm_args.nOptions = 2;
    vm_args.ignoreUnrecognized = JNI_FALSE;

    /* Create the Java VM */
    res = JNI_CreateJavaVM( &(jvm->jvm), (void**)&(jvm->jenv),
    &vm_args );
    #else
    JDK1_1InitArgs vm_args;
    char classpath[1024];
    vm_args.version = 0x00010001;
    JNI_GetDefaultJavaVMInitArgs( &vm_args );
    sprintf( classpath, "%s%c%s",
    vm_args.classpath, PATH_SEPARATOR, &vm_args);
    vm_args.classpath = classpath;

    /* Create the Java VM */
    res = JNI_CreateJavaVM( &(jvm->jvm), &(jvm->jenv), &vm_args);
    #endif

    return res;
    }

    I compile the code as follows

    -----------------------------------------------------------
    JDK_ROOT = C:/j2sdk1.4.2_07
    JNI_HOME = -I$(JDK_ROOT)/include \
    -I$(JDK_ROOT)/include/win32
    DLLTOOL = dlltool
    MINGW_EXTRA = d:/FreeSofts/MinGW
    MINGW_INCDIR = -I$(MINGW_EXTRA)/include
    MINGW_LIBDIR = $(MINGW_EXTRA)/lib


    pxdbJvmTest.exe : pxdbJvmTest.c pxdbjvm.c libjvm.a
    gcc -g -o $@ pxdbJvmTest.c pxdbjvm.c $(JNI_HOME) -L. -ljvm

    libjvm.a: jvm.def
    $(DLLTOOL) -k --input-def jvm.def --dllname jvm.dll \
    --output-lib $@

    jvm.def:
    echo EXPORTS >jvm.def
    echo JNI_CreateJavaVM@12 >> jvm.def
    echo JNI_GetDefaultJavaVMInitArgs@4 >> jvm.def

    clean:
    rm *.exe
    rm *.o
    rm jvm.def
    rm libjvm.a
    ---------------------------------------------------------
    In the pxdbJvmTest.c file, I juset test the return value of InvokeJVM
    function and it is reported to be -1.

    To compile/link and run the problem, I have to copy the jvm.dll from
    c:/j2sdk1.4.2_07/jre/bin/client/ to the working dir.

    Any suggestions?

    Regards
    -Bruce
     
    Bruce, Feb 13, 2006
    #1
    1. Advertising

  2. Bruce

    Chris Uppal Guest

    Bruce wrote:

    > To compile/link and run the problem, I have to copy the jvm.dll from
    > c:/j2sdk1.4.2_07/jre/bin/client/ to the working dir.


    Never move the JVM dll away from where it lives -- it expects to find stuff
    "near" it.

    -- chris
     
    Chris Uppal, Feb 13, 2006
    #2
    1. Advertising

  3. Bruce

    Vitaly Guest

    See http://simtel.net/product.php[id]93174[SiteID]simtel.net
    and
    http://codeproject.com/cpp/OOJNIUse.asp



    "Bruce" <> wrote in message
    news:...
    > Hi all,
    >
    > I have a problem invoking java VM by c code through JNI. The jdk is
    > j2sdk1.4.2_07. I use gcc in MinGW on win XP os.
    >
    > I can compile and link the program correctly, but when I run the result
    > .exe file, it reported that the JNI_CreateJavaVM had failed.
    >
    > The InvokeJVM code is as follows:
    > -----------------------------------------------------------------
    > #include <jni.h>
    >
    > typedef pxdbJVM pxdbJVM;
    > struct pxdbJVM
    > {
    > JavaVM *jvm;
    > JNIEnv *jenv;
    > };
    >
    > struct pxdbJVM pxdbJvm;
    >
    > int InvokeJVM( pxdbJVM *jvm )
    > {
    > jint res = 0;
    >
    > #ifdef JNI_VERSION_1_2
    > JavaVMInitArgs vm_args;
    > JavaVMOption options[2];
    > options[0].optionString = "-Djava.class.path=.";
    > options[1].optionString = "-Xmx64m";
    >
    > memset( &vm_args, 0, sizeof( vm_args ));
    > vm_args.version = JNI_VERSION_1_2;
    > vm_args.options = options;
    > vm_args.nOptions = 2;
    > vm_args.ignoreUnrecognized = JNI_FALSE;
    >
    > /* Create the Java VM */
    > res = JNI_CreateJavaVM( &(jvm->jvm), (void**)&(jvm->jenv),
    > &vm_args );
    > #else
    > JDK1_1InitArgs vm_args;
    > char classpath[1024];
    > vm_args.version = 0x00010001;
    > JNI_GetDefaultJavaVMInitArgs( &vm_args );
    > sprintf( classpath, "%s%c%s",
    > vm_args.classpath, PATH_SEPARATOR, &vm_args);
    > vm_args.classpath = classpath;
    >
    > /* Create the Java VM */
    > res = JNI_CreateJavaVM( &(jvm->jvm), &(jvm->jenv), &vm_args);
    > #endif
    >
    > return res;
    > }
    >
    > I compile the code as follows
    >
    > -----------------------------------------------------------
    > JDK_ROOT = C:/j2sdk1.4.2_07
    > JNI_HOME = -I$(JDK_ROOT)/include \
    > -I$(JDK_ROOT)/include/win32
    > DLLTOOL = dlltool
    > MINGW_EXTRA = d:/FreeSofts/MinGW
    > MINGW_INCDIR = -I$(MINGW_EXTRA)/include
    > MINGW_LIBDIR = $(MINGW_EXTRA)/lib
    >
    >
    > pxdbJvmTest.exe : pxdbJvmTest.c pxdbjvm.c libjvm.a
    > gcc -g -o $@ pxdbJvmTest.c pxdbjvm.c $(JNI_HOME) -L. -ljvm
    >
    > libjvm.a: jvm.def
    > $(DLLTOOL) -k --input-def jvm.def --dllname jvm.dll \
    > --output-lib $@
    >
    > jvm.def:
    > echo EXPORTS >jvm.def
    > echo JNI_CreateJavaVM@12 >> jvm.def
    > echo JNI_GetDefaultJavaVMInitArgs@4 >> jvm.def
    >
    > clean:
    > rm *.exe
    > rm *.o
    > rm jvm.def
    > rm libjvm.a
    > ---------------------------------------------------------
    > In the pxdbJvmTest.c file, I juset test the return value of InvokeJVM
    > function and it is reported to be -1.
    >
    > To compile/link and run the problem, I have to copy the jvm.dll from
    > c:/j2sdk1.4.2_07/jre/bin/client/ to the working dir.
    >
    > Any suggestions?
    >
    > Regards
    > -Bruce
    >
     
    Vitaly, Feb 13, 2006
    #3
    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. Daniel
    Replies:
    0
    Views:
    416
    Daniel
    Jan 28, 2004
  2. Arjen
    Replies:
    7
    Views:
    524
    Arjen
    Apr 7, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,784
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    642
    vasanth
    Jan 25, 2005
  5. Tim
    Replies:
    3
    Views:
    554
    Chris Uppal
    Feb 13, 2006
Loading...

Share This Page