JavaToDPR - Java to Delphi Project (Example)

JavaToDPR - Java to Delphi Project (Delphi Project Stub File Generator)

Sample Output

The Java class below is a simple class that declares three native methods. These methods will be implemented in a Delphi DLL named SimpleDemo.dll.

public class SimpleDemo{
  public static void main(String[] args){
    SimpleDemo sd = new SimpleDemo();
    sd.printHelloJava();
    sd.printHelloDelphi();
    sd.printStringDelphi("From Java to Delphi");
    
    int sum = sd.addIntegers(3, 6);
    System.out.println("3 + 6 = " + sum);
  }

  public void printHelloJava(){
    System.out.println("Hello from Java!");
  }

  native public void printHelloDelphi();
  native public void printStringDelphi(String str);
  native public int addIntegers(int num1, int num2);

  static{
    System.loadLibrary("SimpleDemo");
  }
}
First, we need to compile the Java source file. From the command line, we simply do:
javac SimpleDemo.java
which will produce the file, SimpleDemo.class.

The next step is to run JavaToDPR on the .class file to produce the .dpr file:

java JavaToDPR -o SimpleDemo.dpr SimpleDemo
This will create a file, SimpleDemo.dpr which can be immediately compiled with Delphi to produce a DLL named SimpleDemo.dll. JavaToDPR simply generated the "shell" of the source file, which currently doesn't do anything. The resulting .dpr file looks like this:

library SimpleDemo;

uses JNI;

(*
 * Class:     SimpleDemo
 * Method:    printHelloDelphi
 * Signature: ()V
 *)
procedure Java_SimpleDemo_printHelloDelphi(PEnv: PJNIEnv; Obj: JObject); stdcall;
begin  
end;

(*
 * Class:     SimpleDemo
 * Method:    printStringDelphi
 * Signature: (Ljava/lang/String;)V
 *)
procedure Java_SimpleDemo_printStringDelphi(PEnv: PJNIEnv; Obj: JObject; Arg1: JString); stdcall;
begin
end;

(*
 * Class:     SimpleDemo
 * Method:    addIntegers
 * Signature: (II)I
 *)
function Java_SimpleDemo_addIntegers(PEnv: PJNIEnv; Obj: JObject; Arg1: JInt; Arg2: JInt): JInt; stdcall;
begin
end;

exports
  Java_SimpleDemo_printHelloDelphi,
  Java_SimpleDemo_printStringDelphi,
  Java_SimpleDemo_addIntegers;

end.

In order for the native methods to do something useful, we need to implement the stubs. The code required for this example is very trivial and is highlighted in RED in the code below:

library SimpleDemo;

uses JNI;

(*
 * Class:     SimpleDemo
 * Method:    printHelloDelphi
 * Signature: ()V
 *)
procedure Java_SimpleDemo_printHelloDelphi(PEnv: PJNIEnv; Obj: JObject); stdcall;
begin  
  Writeln('Hello from Delphi!');
end;

(*
 * Class:     SimpleDemo
 * Method:    printStringDelphi
 * Signature: (Ljava/lang/String;)V
 *)
procedure Java_SimpleDemo_printStringDelphi(PEnv: PJNIEnv; Obj: JObject; Arg1: JString); stdcall;
var
  JVM: TJNIEnv;
begin
  JVM := TJNIEnv.Create(PEnv);
  Writeln(JVM.JStringToString(Arg1));
  JVM.Free;
end;

(*
 * Class:     SimpleDemo
 * Method:    addIntegers
 * Signature: (II)I
 *)
function Java_SimpleDemo_addIntegers(PEnv: PJNIEnv; Obj: JObject; Arg1: JInt; Arg2: JInt): JInt; stdcall;
begin
  Result := Arg1 + Arg2;
end;

exports
  Java_SimpleDemo_printHelloDelphi,
  Java_SimpleDemo_printStringDelphi,
  Java_SimpleDemo_addIntegers;

end.
After implementing the methods in the Delphi library, we compile the file into a DLL. Then, to test the whole project, we simply run the Java interpreter and give it the class (SimpleDemo.class) that we wish to load.
java SimpleDemo
which produces this output:
Hello from Java!
Hello from Delphi!
From Java to Delphi
3 + 6 = 9

Copyright © 2001 Matthew Mead. All Rights Reserved.