Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Error invoking JVM through JNI

Thread view: 
Bruce - 13 Feb 2006 07:18 GMT
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
Chris Uppal - 13 Feb 2006 12:27 GMT
> 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
Vitaly - 13 Feb 2006 21:29 GMT
See http://simtel.net/product.php[id]93174[SiteID]simtel.net
and
http://codeproject.com/cpp/OOJNIUse.asp

> Hi all,
>
[quoted text clipped - 91 lines]
> Regards
> -Bruce


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.