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.
First, we need to compile the Java source file. From the command line, we simply do: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"); } }
which will produce the file, SimpleDemo.class.javac SimpleDemo.javaThe next step is to run javadpr on the .class file to produce the .dpr file:
This will create a file, SimpleDemo.dpr which can be immediately compiled with Delphi to produce a DLL named SimpleDemo.dll. javadpr simply generated the "shell" of the source file, which currently doesn't do anything. The resulting .dpr file looks like this:javadpr -o SimpleDemo.dpr SimpleDemo
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:
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.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.
which produces this output:java SimpleDemoHello from Java! Hello from Delphi! From Java to Delphi 3 + 6 = 9