Java4Delphi

Creates Delphi .pas files from Java .class files and Java .jar archives so Java classes and libraries can be used from Delphi code.
  • uses the newest Java Native Interface (JNI)
  • uses classes with automatic reference-counting memory management
  • supports Delphi 7 - 10.1
  • source code of JNI and Delphi4Java units included in registered version

Example

// Generated Delphi code:
type
  Itest_Test = interface(Ijava_lang_Object)
    function hello: string;
  end;

  Ttest_Test = class(Tjava_lang_Object, Itest_Test)
  protected
    function hello: string;
  public
    constructor Create(JavaObject: JObject); overload;
    constructor Create; overload;
  end;

  Ttest_TestClass = class(Tjava_lang_ObjectClass)
  protected
    F_initID: JMethodID;
    FhelloID: JMethodID;
    constructor Create(const ClassName: AnsiString; Initialize: Boolean);
  public
    property _initID: JMethodID read F_initID;
    property helloID: JMethodID read FhelloID;
  end;

var test_TestClass: Ttest_TestClass;

constructor Ttest_Test.Create(JavaObject: JObject);
begin
  Gettest_TestClass;
  inherited Create(JavaObject);
end;

constructor Ttest_Test.Create;
begin
  Gettest_TestClass;
  inherited Create(JNIEnv(Env).NewObjectA(test_TestClass.JavaClass, test_TestClass.F_initID, nil));
  CheckJavaException;
end;

function Ttest_Test.hello: string;
begin
  Result := GetString(Env, JNIEnv(Env).CallObjectMethodA(JavaObject, test_TestClass.FhelloID, nil));
  CheckJavaException;
end;

function Gettest_TestClass: Ttest_TestClass;
begin
  if test_TestClass = nil then
    test_TestClass := Ttest_TestClass.Create('test/Test', True);
  Result := test_TestClass;
end;

constructor Ttest_TestClass.Create(const ClassName: AnsiString; Initialize: Boolean);
begin
  inherited Create(ClassName, False);
  if Initialize then
    F_initID := Java4Delphi.GetMethodID(JavaClass, '<init>', '()V');
  FhelloID := Java4Delphi.GetMethodID(JavaClass, 'hello', '()Ljava/lang/String;');
end;

// Usage:
CreateJavaVM;
ShowMessage(Ttest_Test.Create.hello);
								

Download and order

Related links