Using the Java Native Interface with Delphi

Website

Introduction

The Java Native Interface (JNI) is an API that allows a Java application to interact with code written in other languages, such as C++ and Delphi. These non-Java languages are referred to as native languages because, unlike Java code, the resulting compiled code is machine-dependent. In order for a Java program to interoperate with native code, the native code must be able to compile to a shared library. Under Linux, a shared library generally has lib prepended to its name and has .so appended. Under Windows, a shared library is known as a dynamic link library or DLL, and has .dll appended to its name.

The Java Native Interface is just that: an interface. More specifically, it is a Java API that programmers can write to, instead of writing directly to a vendor-specific virtual machine API. This has the benefit that programmers need only implement one version of their software, rather than a version for each specific VM. Also, since the JNI is part of the Java API, all implementations of the Java VM are guaranteed to support it.

The JNI supports two paradigms or models. The first model allows Java code to access native code that resides in a shared library. This portion of the JNI doesn't have an official name, so we'll just refer to it as the JNI proper. The second model allows a native application to access Java code. This second model is known as the Invocation API and using it within a native application or shared library is known as embedding the Java virtual machine.

These documents were originally written before Kylix, so naturally they were Windows-only. However, most (if not all) of the material applies to Kylix and Linux as well. Rather than re-write a separate document for Kylix (which would be about 98% identical in content) or use the phrases "Delphi or Kylix" and "DLL or SO" everywhere, I've left it Windows-specific. I've tried to point out any differences between the two environments, but may have missed something along the way. When I say Delphi, I am referring to both Delphi and Kylix. When I say DLL, I am referring to both DLLs (.DLL) on Windows and shared libraries (.so) on Linux.

Tutorials

Part One - Introduction and Tutorial This is the place to start if you've never done any JNI programming or haven't done JNI programming with Delphi. It's a detailed tutorial that takes you step-by-step through the process of calling a Delphi DLL from Java code. It is the Hello World! program of the JNI.

Part Two - Techniques and Examples After completing Part One, you can move on to more complex interactions between Java and Delphi code. This part demonstrates several techniques such as creating Java objects in Delphi, passing two-dimensional arrays, exceptions, etc.

Part Three - The Invocation API This portion of the JNI is what allows Delphi to call into Java code. It demonstrates a Delphi GUI application that calls Java code to download URLs over the Internet. It also shows how to create and use multiple Java objects in background threads.

Delphi/JNI on the Web

Delphi/JNI Website I try to keep this site up to date. It basically contains the documentation you find here but may be updated more frequently.

JavaToDPR - A tool (written in Java) to create a Delphi .dpr file from a Java .class file. This is sort of analogous to the javah tool that comes with the JDK. There's also a Delphi version called javadpr. Both are free from my website and may help you get started writing native code that is to be called from Java.

If you are interested in programming with the Java Native Interface, do yourself a favor and get Sheng Liang's book. (He designed the JNI.) It is the Bible of JNI programming. The code is in C/C++ but translates to Delphi code almost one-to-one. There is a wealth of information on all aspects of using the Java Native Interface. You will save yourself from hours of frustration. Worth every penny.

The Java Native Interface: Programmer's Guide and Specification by Sheng Liang (Addison-Wesley Publishing Company, June 1999. ISBN 0201325772)


Copyright © 2000-2002 Matthew Mead All Rights Reserved.