aleroot / DelphiJNI

A Delphi/Kylix JNI implementation .
30 stars 8 forks source link

Class not found! #2

Closed hafedh-trimeche closed 2 years ago

hafedh-trimeche commented 2 years ago

Hello,

Please note that JNI can't find the class PageGrabber from PageGrabber.zip

procedure doJNI;
label
  Clear;
var
  Errcode : Integer;
  VM_args : JavaVMInitArgs;
  Options : array[1..10] of JavaVMOption;
  JavaVM  : TJavaVM;
  JavaEnv : TJNIEnv;
  JavaCls : JClass;
begin
  LoadJVM(JavaLibPath);
  InitRecord(Options,SizeOf(Options));
  VM_args.version            := JNI_VERSION_1_8;
  VM_args.options            := @Options;
  VM_args.nOptions           := 1;
  VM_args.ignoreUnrecognized := True;
  Options[1].optionString    := '-Djava.class.path=.';
  JavaVM                     := TJavaVM.Create(VM_args.version);
  JavaEnv                    := nil;
  try
    Errcode := JavaVM.LoadVM(VM_args);
  finally
  end;
  if Errcode<0 then
  begin
    ShowMessageFmt('LoadVM failed, error code = %d', [Errcode]);
    goto Clear;
  end;
  JavaEnv := TJNIEnv.Create(JavaVM.Env);
  if JavaEnv=nil then
  begin
    MessageBox(Handle,'Create Java Env Error','Delphi Java Class',MB_OK or MB_ICONERROR);
    goto Clear;
  end;
  JavaCls := JavaEnv.FindClass('PageGrabber');
  if JavaCls=nil then
  begin
    MessageBox(Handle,'Create Java Class Error','Delphi Java Class',MB_OK or MB_ICONERROR);
    goto Clear;
  end;
Clear:
  if Assigned(JavaEnv) then FreeAndNil(JavaEnv);
  if Assigned(JavaVM) then FreeAndNil(JavaVM);
end;

Best regards.

aleroot commented 2 years ago

I think your application is having a problem to load the java classes from the classpath... I would compile the PageGrabber.java and put the resulting PageGrabber.class to the same location of the Delphi executable, then you can pass the classpath string like this:

Options[0].optionString := PAnsiChar(AnsiString('-Djava.class.path=' + ExtractFilePath(ParamStr(0))));

And the options array should be declared like: Options: array [0 .. 4] of JavaVMOption;