Name: bsC130419 Date: 07/12/2001
java version "1.4.0-beta"
I'm trying to create 2 JVMS in a C++ program by using multiple threads. Each of
threads tries to start a java virtual machine by calling JNI_CreateJavaVM. Only
one of the threads can start java virtual machine successfully, the other one
gets return value -5 from calling JNI_CreateJavaVM. There are references to not
being able to create more than a single JVM from a single process with JDK 1.1
but I can not find anything about JDK 1.2.
The following are the code for the test.
/*_thread.h*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#ifndef _REENTRANT
#define _REENTRANT
#endif /* ifndef _REENTRANT */
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <process.h>
#define sleep(n) Sleep ( n * 1000 )
#define dtLARGE int
typedef void* (*funcPtr) ( void* ) ;
typedef funcPtr thrFuncPtr ;
typedef struct _threadStruct
{
DWORD threadId ;
HANDLE handle ;
thrFuncPtr func ;
/* Argument pointer for func function */
void* args ;
}
threadStruct , *threadPtr ;
typedef DWORD key ;
typedef void (*destructorFn)( void *value) ;
dtLARGE KeyCrt ( key* k, destructorFn d ) ;
dtLARGE KeyGet ( key k, void** value ) ;
dtLARGE KeySet ( key k, void* value ) ;
#ifdef __cplusplus
}
#endif
/*end of _thread.h*/
/*SqlJInterface.cpp*/
#include <jni.h>
#include <stdlib.h>
#include <string.h>
#include "_thread.h"
extern destructorFn destArray[];
extern "C" {
unsigned int WINAPI threadTestM(void * i);
}
dtLARGE ThrCrt ( threadPtr tPtr )
{
if ( ! ( tPtr->handle = (HANDLE)_beginthreadex ( '\0', 0, &threadTestM,
tPtr,0,(unsigned int *)&tPtr->threadId ))) return 0 ;
return 0 ;
}
void startM()
{
JavaVM *jv;
JNIEnv *env;
jint res;
JavaVMInitArgs vm_args;
JavaVMOption options[2];
options[0].optionString = "-Djava.compiler=NONE";
char const* clspath = getenv("CLASSPATH");
if(clspath)
{
char* p1 = "-Djava.class.path=";
options[1].optionString = new char[strlen(p1)+strlen(clspath)
+1];
strcpy(options[1].optionString,p1);
strcat(options[1].optionString,clspath);
}
else
{
printf( "Classpath may not be set up for java virtual
machine\n") ;
}
vm_args.nOptions = 2;
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jv,(void**)&env,&vm_args);
if (res < 0) {
printf( "Can not start java virtual machine. The return value
is: %d\n",res) ;
}
else
printf( "java virtual machine is started\n") ;
}
void startD(int p)
{
JavaVM *jv;
JNIEnv *env;
jint res;
JavaVMInitArgs vm_args;
JavaVMOption options[5];
options[0].optionString = "-Xdebug";
options[1].optionString = "-Xnoagent";
options[2].optionString = "-Djava.compiler=NONE";
char const* clspath = getenv("CLASSPATH");
if(clspath)
{
char* p1 = "-Djava.class.path=";
options[3].optionString = new char[strlen(p1)+strlen(clspath)
+1];
strcpy(options[3].optionString,p1);
strcat(options[3].optionString,clspath);
}
else
{
printf( "Classpath may not be set up for java virtual
machine\n") ;
}
options[4].optionString = "-
Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y";
vm_args.nOptions = 5;
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jv,(void**)&env,&vm_args);
if (res < 0) {
printf( "can not start java virtual machine\n") ;
}
else
printf( "java virtual machine get started\n") ;
}
unsigned int WINAPI threadTestM(void * i)
{
int p = *((int*)i);
printf( "Enter threadTestM with i = %d\n",p) ;
startM();
return 0;
}
main()
{
int p = 10;
threadStruct myThread;
myThread.args = (void*)&p;
ThrCrt(&myThread);
Sleep(1000);
int pp =20;
myThread.args = (void*)&pp;
ThrCrt(&myThread);
Sleep(1000);
return 0;
}
/*end of SqlJInterface.cpp*/
(Review ID: 127932)
======================================================================