Michael Edwards
Developer Technology Engineer
Microsoft Corporation
Updated October 7, 1997
The following article was originally published in the Site Builder Network Magazine, which is now MSDN Online Voices.
If you write applications for Microsoft Windows but would rather be coding in Java, life is about to get a whole lot better. A new feature of Microsoft's virtual machine (VM) for Java, called J/Direct, allows Java code running on the Microsoft Win32 VM for Java to directly call Win32 functions. All you need to know is how to make the call in C.
Or maybe your job has you writing Java code for multiple platforms. Then J/Direct may also make your life a lot easier. Unfortunately, the Java Development Kit (JDK) 1.1 API doesn't deliver all the goods you can get today on most platforms. In the meantime, Java developers must improvise to access important native functionality.
That's why Microsoft's Java team created J/Direct. Maybe you have read elsewhere about all kinds of horrible havoc Microsoft is supposedly trying to wreak on the Java world. But for a professional developer, what matters is extending the productivity of Java to provide competitive products on the platforms your customers demand.
Check the links under "Microsoft Enhancements for Java" on the Microsoft SDK for Java 2.0 page for details on the options currently available to Java developers who wish to utilize Windows functionality in their applications. Before J/Direct, these options boiled down to three main areas:
Here's the problem that J/Direct solves:
The packages and classes that Microsoft already provides in the Microsoft SDK for Java 2.0 are one way to access Windows functionality. These are new Java classes that let you use Windows functionality from Java without writing any C code. But they do not cover all aspects of the Win32 API, nor are all those aspects that are covered provided in precisely the way a Java developer might like.
Java and COM integration is great, but the Win32 API is implemented in DLLs, so you can't get at it directly from Java source through this technique. That leaves RNI as the direct way to get at the Win32 API from Java source code--but that won't work either, because RNI is intended for Java developers who wrote their own DLLs, and want to access them from their Java sources.
J/Direct, by providing a way to call functions exported from any Windows DLL, addresses the need for Java developers to access the full Win32 API directly from their Java code.
But using the Win32 API directly from Java can cause sticky problems, right? Windows was written to be accessed from C, and, because of the many fundamental differences between C and Java data types, it's tricky to build parameter lists and receive return values. There are compatibility problems: Java does not have pointers or a representation for unsigned numbers, Java strings don't look like C strings, and Java doesn't have an analog for the C structure data type. There are problems in calling conventions, as well--such as how to invoke Win32 functions that use callbacks, or who allocates memory buffers. To help with these problems, J/Direct provides a set of guidelines and helper classes that outline the best ways to interface with the Win32 API from Java code.
If you need to access functionality provided by an existing Windows DLL, and you already know how to use that DLL, J/Direct lets you get the job done.
Some people develop applications in Java in pursuit of cross-platform independence. Why should they care one whit about a Microsoft initiative that ties them to the Windows platform?
Consider two fundamental realities of developing software applications.
First, timing is everything. Whether you are developing a new product category, or gunning against competitors in a bid to be the best, you are always in a race. The first person to market with a good product has a huge advantage. We have been beaten to market at Microsoft, and it feels awful. Never mind that you have superior ideas. You don't win market share when you're waiting for your tool vendor to provide proven cross-platform APIs.
Second, what separates the winners from the losers is successful risk management. If, at least for now, you must utilize native code to implement a successful product, then you want to choose your risks very carefully. J/Direct allows you to minimize your risks in a key area, because any time frittered away just accessing the Win32 API is wasted. It's time you could have spent making your product better, or shipping it sooner.
Microsoft has added the necessary run-time pieces for J/Direct to the version of the Microsoft Win32 VM for Java shipped in Internet Explorer 4.0. Also, J/Direct is available in the Microsoft SDK for Java 2.0. This version of the Microsoft Win32 VM for Java is also compatible with Internet Explorer 3.02.
Documentation, including an FAQ, and code for J/Direct are available from the Microsoft Java Web site.
Michael Edwards is a development engineer, MSDN Online technology writer, and budding Java developer. He has so many kids at home that Java code seems elementary.
PCWEEK Online says J/Direct enables Java programmers to create Windows applications that could previously only be written in other languages.
Wired says that J/Direct means "a more powerful tool that may be hard for programmers to resist."
J/Direct is available in Microsoft's Software Development Kit for Java 2.0, which you can freely download. It includes the latest release of Microsoft's VM for Java and Microsoft Application Foundation Classes (AFC).