Using the Java Native Interface with Delphi

Matthew Mead

Home - News and Updates
Part One - Introduction and Tutorial
Part Two - Techniques and Examples
Part Three - The Invocation API

The latest official version of JNI.pas is dated November 1, 2002 and includes support for JDK 1.1.x, 1.2.x, 1.3.x, and 1.4.x on both Windows (Delphi) and Linux (Kylix). View it. Download it. (Includes JNI_MD.INC)

The complete updated package (JNI.pas, documentation, tutorials, sample source, etc.) can be downloaded here it.

Note for Delphi 5 Users
The JNI.pas file contains compiler directives to deal with the differences between Linux (Kylix) and Windows (Delphi). Specifically, there are directives: {$IFDEF MSWINDOWS} and {$IFDEF LINUX}, which indicate the platform. Kylix defines LINUX and Delphi 6 and 7 define MSWINDOWS. Delphi 5 does not define MSWINDOWS, so you need to define this manually. To define this in Delphi 5, go to Project | Options | Directories/Conditionals and add MSWINDOWS to the "Conditional defines".

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 and may help you get started writing native code that is to be called from Java.
JavaD - A tool similar to the one above in that it generates Delphi code from a Java class file. It can be run stand-alone from the command line, or it can be integrated into the JBuilder IDE by simply placing the JAR file into your JBuilder/lib/ext directory and restarting JBuilder. It was created by Keith Wood for his September 2003 Delphi Informant article about using Java Native Interface with Delphi.

Recommended JNI Book
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)

I've recently discovered that Sun has made this book available online for free in both HTML and PDF form.

News and Updates

February 26, 2005
Daniel U. Thibault has updated the JavaToDPR tool to add functionality for inner classes, embedded underscores and dollar signs in identifiers, and overloaded native methods. You will find the updated files on the JavaToDPR web page. There is also a PDF document that explains the added features in detail.

June 4, 2004
Keith Wood (and the Delphi Informant) has graciously allowed me to post his articles about using the Java Native Interface with Delphi. The articles originally appeared in the September and October issues of the Delphi Informant magazine. You can read his articles here. Be sure to check out his JBuilder plug-in (JavaD) for generating Delphi skeleton code for native methods. Direct links to Part 1 and Part 2 of the series.

September 3, 2003
The September issue of the Delphi Informant magazine has published an article about using the Java Native Interface with Delphi. The article, which references the pages on this site, is part one of a two-part series and is written by Keith Wood, a long-time contributor to the Delphi Informant. Keith's article includes a nifty JBuilder plugin (OpenTool API) that generates a Delphi (Pascal) skeleton for the native methods (similar to my JavaToDPR tool), but is more conveniently accessible from the JBuilder IDE. Unfortunately, you have to subscribe to the magazine to read the article online. However, you can download all of the source code for the examples and the JBuilder plugin from here. I suspect that part two will be in next month's issue.

November 1, 2002
It was brought to my attention that there was some missing code in JNI.pas. The TJNIEnv.NewObject method had been left unimplemented since the first version. I vaguely remember this situation. This method expects a variable number of parameters, which is a feature that the Delphi language doesn't provide. Delphi does provide a similar feature: the array of const. Although these two constructs are not directly compatible, it is possible to allow the Delphi user to pass an array of const to the wrapper method, and then have the wrapper method deal with packing the parameters into another construct. This was only a minor irritation for the user, since it had always been possible to call other methods to accomplish the same task that the NewObject method performs.

August 6, 2002
I've finally made the changes "official" and marked the latest JNI.pas file as the current version. I made the updated JNI.pas file available four months ago, but at that time, I didn't have any more time to update and test the sample programs. I've now finished reworking them to make sure that everything still works and is accurate. I was going to wait a little while longer and run it through some tests with Kylix 3, but I can't guarantee that I'll have the time later. So, I'm releasing it all now and I expect it to all work fine with Kylix 3 (and Delphi 7 announced today). Again, the changes made to support the JSDK 1.4 are relatively minor. You can read about them here. When I get Kylix 3 (any day now), I'm hoping to be able to add some Kylix-specific information to the tutorials. Specifically, configuring the environment for different JREs. This seems to be the most confusing part of getting Kylix (and Delphi as well) working with Java. Also, there is an issue concerning Unicode strings that was brought to my attention recently. The final solution will need to work under Linux as well, and I am researching it.

March 30, 2002
A few changes were made to the JNI.pas file, mainly to support the additional JNI features that were added to the Java 2 SDK v1.4. You can read about the enhancements to v1.4 here. JNI.pas is compatible with the older versions of the JDK. Part 1 of the tutorial works as-is. Part 2 needs a slight modification because of an error in the Delphi code that was uncovered by JSDK 1.4 and Part 3 needs an IFDEF to distinguish between versions of the J2SDK. I have completed these modifications and am now updating the tutorial to reflect the changes. I'm also adding some Kylix-specific stuff to the tutorials to help aid in using the JNI with Kylix as well. I've decided to go ahead and release the updated JNI.pas since I've tested the demos with it and all of it works. I just need to make the demo/tutorial changes available.

August 1, 2001
Last week, I attended the 2001 Borland Developer's Conference (BorCon 2001). Earlier this year, in a response to a Call For Papers, I submitted a paper based on these Java Native Interface tutorials. Borland's acceptance of my paper allowed me free entry into the conference. It was the first BorCon that I have ever attended and I enjoyed every minute of it. I wrote an article about the experience, which is posted on Project JEDI's web site. One of the noteworthy news items at this year's conference was that Borland awarded the 2001 Spirit of Delphi Award to Project JEDI.

May 9, 2001
I've developed a couple of new tools to help Delphi programmers translate Java native method declarations into Object Pascal code. Both tools are used to create a Delphi project file (.dpr) from a Java class (.class) file. The first tool, javadpr, is a native Windows executable, much like Sun's javah tool. javadpr is written in Delphi, so it is a simple matter of compiling it with Kylix to get a native Linux executable. The second tool is actually a Java class named JavaToDPR, which is sort of a Java implementation of javadpr. I wrote the Delphi version first, and then decided to do the Java version. The Java version is much cleaner, due to the fact that programming with the JNI incurs some overhead. As always, I've included the source (Java and Delphi) for both tools. They are not meant to be model examples of Java/Delphi code, but are intended to compare/contrast the way the tools are implemented. This is the first version of the tools and I expect that there are issues to address. Please send any comments, questions, suggestions, etc., to me. Also, a big thanks to Jeffrey Schwartz who suggested the whole thing and showed me how to do it in Java.

March 22, 2001
By now most of you have heard that Borland has released Kylix. This means that there is now a Linux-compatible JNI.pas file for those of you looking to work with Delphi and Java in Linux. The JNI.pas file supports both Windows and Linux (via $IFDEF), so you do not need separate unit files. You can download the whole Delphi/Kylix package here. The package contains the update JNI.pas file as well as an updated version of Part 3 of the tutorial, which is not on this site yet. This package is posted on Project JEDI's site along with other Linux API Translations. Special thanks go to Robert Marquardt and David Body who tested it under Kylix. Unfortunately, I do not have Kylix yet, so I am personally unable to do additional work/testing under Kylix. However, if anyone would like to donate a copy of Kylix to me, I'd be happy to accept it!

January 22, 2001
I posted a zip file containing the files for the URLGrabber project in Part 3 that shows a more "real world" use of the Invocation API. The code for the project had been available in the HTML documents, but the .dfm file was missing, which made it more difficult to build the project. Now, you should be able to build and run the project after downloading the zip file.

January 4, 2001
Part 3 of the tutorial has been posted. It's pretty much complete except for a zip file containing all of the source code. However, most of the code is available on the pages, so you can at least copy and paste it into the Delphi IDE, if you want to build it. This part of the Java Native Interface has generated a lot more questions than the other parts. I hope this tutorial answers some of them.

January 2, 2001
JNI.pas has been updated to fix a bug in the TJNIEnv.ArgsToJValue method. This prevented you from passing Delphi strings in open array parameters. The version of JNI.pas that is included with the zip files has not been updated, since the code in those samples do not require this functionality. You can get the latest JNI.pas file here. Also, Part 3 of the series is just about complete and I hope to post it this week. I just need to double-check it to make sure that it is correct.

December 24, 2000
Today, the translation of the Java Native Interface was officially posted on Project JEDI's website. This is the first cross-platform translation to be posted. This was made possible because of Java and Delphi, which both run on Windows and Linux platforms. Technically, the translation is only dual-platform at this time, since only two platforms are supported. Special thanks go to Robert Marquardt from Project JEDI who was instrumental in helping me get the JNI translation in shape to receive JEDI+ Certification.

December 3, 2000
A user pointed out that there were errors in jni.pas. The first parameter to the functions DestroyJavaVM, AttachCurrentThread, DetachCurrentThread, and GetEnv was incorrect. All the files have been updated to reflect the correction.

November 5, 2000
The conversion of jni.h to jni.pas has been submitted to Project JEDI for approval/certification and it is likely that the code will be included on their website at some point in the near future. Because of this, the tutorials and examples have been updated to reflect the changes made for Project JEDI. This makes some of the existing code incompatible with the newer code. The changes are minor enough that a simple search-and-replace could update the old code. However, since the JEDI-certified code will be made widely available, it only makes sense that any documents pertaining to that code should be updated to reflect these changes.

During the course of these updates it is very possible that I made some mistakes and haven't detected them yet. If anyone finds a problem with these new pages, please let me know so I can fix them.

September 27, 2000
An astute reader pointed out that there was a problem with the jni.pas file that was included in the file. The YAPP'ed version of jni.pas is correct, though. I've fixed the problem and replaced the zip file. The incorrect file prevented the project from being compiled. (But that's nit-picking... <g>)

September 18, 2000
Part Two of Using the Java Native Interface with Delphi is finally ready! I must admit, though, that Part Two is a little less polished than Part One. The reason is simple: I've got a lot of other projects I'm working on! But, I need to say "Hey, this isn't perfect (or even finished) yet, but I've got to make this available and move on to something else..."

September 8, 2000
The Delphi translation of the Java Native Interface API (jni.pas) is now in the process of being modified to adhere to the guidelines set forth by Project JEDI. What does this mean? Well, for one thing, if all goes well, it means that more Delphi programmers will have access to this powerful API. Now you are probably asking yourself, "How can I help?" The best thing fellow Delphi programmers can do is to go to Project JEDI's website and become a member. You will be able to choose from many different teams (translating APIs, writing tutorials, documentation, testing, educating others, etc.) If you have any questions about helping with this project, you can email me or get involved with Project JEDI directly.

Copyright © 2000-2002 Matthew Mead. All Rights Reserved.