JNI_CreateJavaVM doesn't work for GUI

J

James Cassidy

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 );
}

}
 
J

Jim Sculley

James said:
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.
 
J

James Cassidy

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

Manfred Rosenboom

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
 
J

James Cassidy

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

Roedy Green

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
 

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

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top