Transfer: Calling Java functions through JNI in Delphi 10.3

Many SDKs now provide JAVA interfaces. But there is no Delphi interface.

There is no way to save the country by curve. We can call Java functions in Delphi 10.3 through JNI.

Step 1: Create a Java virtual machine and operating environment

procedure TForm1.FormCreate(Sender: TObject);
var
Options: array [0 .. 4] of JavaVMOption;
VM_args: JavaVMInitArgs;
ErrCode: Integer;
begin
{ 创建 Java 虚拟机 }
FJavaVM := TJavaVM.Create(JNI_VERSION_1_8);
Options[0].optionString := PAnsiChar(AnsiString('-Djava.class.path=' + ExtractFilePath(ParamStr(0)) + 'classes'));
VM_args.version := JNI_VERSION_1_8;
VM_args.Options := @Options;
VM_args.nOptions := 1;
VM_args.ignoreUnrecognized := True;
ErrCode := FJavaVM.LoadVM(VM_args);
if ErrCode < 0 then
begin
MessageBox(Handle, 'Create Java VM Error', 'Delphi 10.3 调用 Java Class', MB_OK OR MB_ICONERROR);
Halt;
Exit;
end;

{Create Java Virtual Machine Operating Environment}
FJavaEnv: = TJNIEnv.Create (FJavaVM.Env);
if FJavaEnv = nil then
begin
MessageBox (Handle, 'Create Java Env Error', 'Delphi 10.3 Call Java Class', MB_OK OR MB_ICONERROR );
Exit;
end;
end;
Step 2: Execute Java function 

procedure TForm1.btn1Click (Sender: TObject);
var
jcls: JClass;
strClass : UTF8String; strMetod: UTF8String;
strSign:
UTF8String;
strArg, strResult: string;
begin
{query Java class name}
strClass: = 'com / test / javafordelphi / JavaClassForDelphiTest ';
jcls: = FJavaEnv.FindClass (strClass);
if jcls = nil then
begin
ShowMessage (' cant find java class');
Exit;
end;

{Java function name, parameter type, parameter}
strMetod: = 'goTest'; // function name
strSign: = 'String (String)'; // parameter type, return value type
strArg: = '123'; // input parameter

{execute Java function}
strResult: = CallMethod (FJavaEnv, jcls, strMetod, strSign , [strArg], True);
strResult IF <> '' the then
the begin
the MessageBox (the Handle, the PChar (the Format ( 'JavaClassForDelphiTest.goTest the Result: S%', [strResult])), 'Delphi Class 10.3 calling the Java', OR MB_OK MB_ICONINFORMATION);
End;
End;
first Three steps: destroy the virtual machine

procedure TForm1.FormDestroy (Sender: TObject);
begin
FJavaEnv.Free;
FJavaVM.DestroyJavaVM;
FJavaVM.Free;
end;
add JNI, JNIUtils unit reference.

Add two member variables:

  private
    FJavaVM : TJavaVM;
    FJavaEnv: TJNIEnv;
 

Compile and pass.

 

Don't rush. First confirm whether you have installed JAVA, and whether the path of Java is included in the system search path.

The default is (x86):

C:\Program Files (x86)\Java\jdk1.8.0_202\bin

C:\Program Files (x86)\Java\jdk1.8.0_202\jre\bin\server

If not, add them to the Windows system search path (environment variable: Path).

carried out.
————————————————
Copyright Statement: This article is an original article by CSDN blogger "dbyoung", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprint .
Original link: https://blog.csdn.net/dbyoung/java/article/details/89085406

Guess you like

Origin www.cnblogs.com/timba1322/p/12678555.html