Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Delphi library for interoperability with Java using WINSOFT JavaBridge

Intro

Java Native Interface is a standard programming interface for writing Java native methods. Most JNI developers utilize C/C++ but most of the JNI header files ported to Delphi.

In a simple word: Java Native Interface (JNI) is the Java interface to non-Java code. And when you work with data types you should always convert data between Delphi and Java.

JavaBridge library WINSOFT offers you to interoperate with Java using JNI. This is a commercial library and if you wish to utilize the JavaBridge, you must register a license.

Overview

Why use Java Native Interface?

Through JNI interface pointer, native code can:

Installation/Configuration

After downloading the library you should add the library path. Here you can watch a short tutorial on how to configure them

Development

The next step is to open the Examples folder, then Delphi, and you can select either Java4Delphi or Delphi4Java folder there you can execute build32.bat file which creates Test.exe or Test.dll file for you.

Let’s try to convert Java into Delphi.

Start build32.bat file. This batch file creates an executable file using Test.dpr file and connects Test.java source code.

Here is how we are calling the Java code and using the JavaBridge library in Delphi:

program Test;
{$APPTYPE CONSOLE}
uses
  SysUtils, JNI, JavaBridge;

var
  Jvm, Env: Pointer;

type
  TTest = class
  private
    FCls: JClass;
    FObj: JObject;
    FInit: JMethodID;
    FHello: JMethodID;
    FValue: JFieldID;
    function GetValue: Integer;
    procedure SetValue(Value: Integer);
  public
    constructor Create;
    procedure Hello(const Text: string);
    property Value: Integer read GetValue write SetValue;
  end;

constructor TTest.Create;
begin
  inherited Create;
  FCls := JNIEnv(Env).FindClass('test/Test');
  FInit := JNIEnv(Env).GetMethodID(FCls, '<init>', '()V');
  FHello := JNIEnv(Env).GetMethodID(FCls, 'hello', '(Ljava/lang/String;)V');
  FValue := JNIEnv(Env).GetFieldID(FCls, 'value', 'I');
  FObj := JNIEnv(Env).NewObjectA(FCls, FInit, nil);
end;

function TTest.GetValue: Integer;
begin
  Result := JNIEnv(Env).GetIntField(FObj, FValue);
end;

procedure TTest.SetValue(Value: Integer);
begin
  JNIEnv(Env).SetIntField(FObj, FValue, Value);
end;

procedure TTest.Hello(const Text: string);
begin
  JNIEnv(Env).CallNonvirtualVoidMethodA(FObj, FCls, FHello, PJValue(Args(Env, [CreateJString(Env, Text)])));
end;

var
  InitArgs: JavaVMInitArgs;
  MyTest: TTest;

begin
  JavaBridge.LoadLibrary;

  FillChar(InitArgs, SizeOf(InitArgs), 0);
  InitArgs.version := JNI_VERSION_1_6;
  if JNI_CreateJavaVM(Jvm, Env, InitArgs) <> 0 then
    raise Exception.Create('Cannot initialize JavaVM');

  MyTest := TTest.Create;
  WriteLn('Value = ' + IntToStr(MyTest.Value));
  MyTest.Value := 10;
  WriteLn('Value = ' + IntToStr(MyTest.Value));
  MyTest.Hello('Hello, world!');
end.

And Test.java source code:

package test;
class Test {
	public void hello(String text) {
		System.out.println(text);
	}
	public int value = 1;
}
procedure TTest.Hello(const Text: string);
begin
  JNIEnv(Env).CallNonvirtualVoidMethodA(FObj, FCls, FHello, PJValue(Args(Env, [CreateJString(Env, Text)])));
End;

Delphi for Java demo is a bit different, it generates you to a dynamic link library. Then you can call native (DLL) code from Java using JNI.

You can check how it is creating an executable file and dynamic link library by opening the build32.bat batch file.

if "%PROCESSOR_ARCHITECTURE%" == "AMD64" GOTO Win64
if "%PROCESSOR_ARCHITEW6432%" == "AMD64" GOTO Win64

:Win32

set JDK=%ProgramFiles%\Java\jdk1.8.0_261\bin
set DELPHI=%ProgramFiles%\Embarcadero\Studio\21.0\bin

goto Next

:Win64

set JDK=%ProgramFiles(x86)%\Java\jdk1.8.0_261\bin
set DELPHI=%ProgramFiles(x86)%\Embarcadero\Studio\21.0\bin

:Next

call clean.bat

"%JDK%\javac" -g:vars test\Test.java
"%DELPHI%\dcc32" -NSSystem;Vcl;Winapi;System.Win -U..\..\..\Delphi104 Test.dpr
"%JDK%\java" test.Test

Head over and check out the full WINSOFT JavaBridge library.

Like what you see? You can get the JavaBridge Library and over 100 other fantastic WinSoft components with our Enterprise Component Pack. For a limited time, when you purchase RAD Studio Enterprise or Architect Edition at special Upgrade Pricing, you will also get this package of third-party software worth over $13,000, including the full WinSoft Component Library, at NO EXTRA COST! Step up to RAD Studio 10.4.1 today!

Exit mobile version