Thank you that helped - I was using JavaVM.GetCreatedJavaVMs rather
than JNI_GetCreatedJavaVMs.
I have included the code i am using because i need to find the javaVM
environment so that the JNIEnv can be created. I have tried to use
the GetEnv method but i get an access violation. I would like the
createJVM method below to check if a JVM exists and create one if it
doesn't otherwise create the JNI environment with the existing JavaVM
that is already running.
If you can help me - it might stop me jumping out of a window!!
cheers
function CreateJVM(FJNIEnv: TJNIEnv) : TJNIEnv;
var
Errorcode : Integer;
VM_args : JavaVMInitArgs;
EnvVarStr, JVMOptionStr : String;
JVMOptionChar : PAnsiChar;
Options: array [0..10] of JavaVMOption;
FJavaVM: TJavaVM;
vms: array[0..1024] of pJavaVM;
ExistingJavaVM : PJavaVM;
TotalVMs : JSize;
PEnv : PJNIEnv;
IntRet : JInt;
begin
try
JNI_GetCreatedJavaVMs(ExistingJavaVM,1,TotalVMs);
if TotalVMs <= 0 then begin
// Create the wrapper for the VM
FJavaVM := TJavaVM.Create;
try
// Get the Environment variable that holds all the class paths
for
// the java classes.
EnvVarStr := GetEnvironmentVariable( EnvironmentVariable );
JVMOptionStr := '-Djava.class.path=.;' + EnvVarStr;
JVMOptionChar := strAlloc( pEnvOptionMaxVarSize + 1 );
// Convert the java option type string to a Char type
StrPCopy( JVMOptionChar, JVMOptionStr );
// Set up the options for the VM
FillChar(Options, SizeOf(Options), #0);
Options[0].optionString := JVMOptionChar;
{$IFDEF USING_J2SDK1_4}
VM_args.version := JNI_VERSION_1_4;
{$ELSE}
VM_args.version := JNI_VERSION_1_2;
{$ENDIF}
VM_args.options := @Options;
VM_args.nOptions := 1;
// Load the VM
Errorcode := FJavaVM.LoadVM(VM_args);
if Errorcode < 0 then
begin
RaiseAnException(Errorcode);
end;
// Create the Env class
FJNIEnv := TJNIEnv.Create(FJavaVM.Env);
Result := FJNIEnv;
// Free the memory for the class path char type
StrDispose( JVMOptionChar );
finally
FJavaVM.Free;
end;
end
else begin
try
FJavaVM := TJavaVM.Create;
FJavaVM.JavaVM := ExistingJavaVM;
// ???
IntRet := FJavaVM.JavaVM^.GetEnv(ExistingJavaVM,PEnv,JNI_VERSION_1_2);
FJNIEnv := TJNIEnv.Create(FJavaVM.Env);
Result := FJNIEnv;
finally
FJavaVM.Free;
end
end;
except
on E: Exception do begin
if ( E is ESavJavaTechnologyException ) then
raise
else
raise ESavJavaTechnologyException.createBase(
'',
'An unknown error has occurred whilst creating the Java
Virtual Machine ' +
E.message,
'',
'',
'',
E );
end;
end;
end;