JNI for Delphi : Help with GetCreatedJavaVMs

Z

zorba

After finding out that the DestroyJavaVM method is not supported, i
have tried to check if the JVM has already been created by using the
GetCreatedJavaVMs method but i am not having any luck in making it
work.

Can anyone explain what i have to do to make it work or direct me to
some examples of it being used.
I am using the JNI within Delphi to call java methods.

Cheers
 
G

Gordon Beaton

After finding out that the DestroyJavaVM method is not supported, i
have tried to check if the JVM has already been created by using the
GetCreatedJavaVMs method but i am not having any luck in making it
work.

What have you tried? Show some code.

This is all you need to do:

jsize count;
JavaVM *jvm;

JNI_GetCreatedJVMs(&jvm, 1, &count);

On success, "count" contains the number of JVMs returned.

Alternatively you could simply keep the original JVM pointer you got
when you created the JVM, and refer to that later in the code.

/gordon
 
Z

zorba

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;
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top