Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

The Win32-Java hybrid

Worried that your Java apps won't run on Windows? Here's a strategy that blends the best of Java with Win32 to create applications that run on Microsoft OSs

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 2

The text editor application is called te. The Java section of the program was built using JDK 1.2. The C portion was built using Borland C++ 4.52. (Yes, you can write meaningful WJH applications using a non-Visual C++ development tool.)

Table 2 identifies the source and executable files that constitute the Java section of the program. These files are distributed in an archive called te-java.zip. To download these sample code files, click here.

Table 2. Java files


File Name Description
te.class Executable file
te.java Source file


Table 3 identifies the source and executable files that constitute the C portion of the WJH. These files are distributed in an archive called te-c.zip. To download these sample code files, click here.

Table 3. C files


File Name Description
jni.h JNI header file (comes with JDK)
jni_md.h JNI header file (comes with JDK) -- slightly modified for use with Borland C++
te.c Source file
te.exe Executable file
te.ide Project file


If you want to learn how te works, first study te.c (the source for te.exe) and then te.java (the source for te.class).

For the purposes of this article, it isn't necessary to go through all of te.c, since it contains a generous helping of comments. However, there are a few observations worth noting.

As you probably know, there are two kinds of Windows applications: console-based and GUI (graphical user interface)-based. This example utilizes a console-based format for te.exe; however, it doesn't matter whether or not you choose console or GUI as your compiler option when creating WJH applications, because the Win32 part of the app wouldn't normally register window classes, create windows, or contain messaging loops.

In the example, we disabled Ctrl-Break key presses so that they won't disrupt te. You can do this by calling the Win32 SetConsoleCtrlHandler API. Without this call, the JVM could abort while executing Java instructions and remaining code in the Win32 executable wouldn't be reached. Do you have to disable Ctrl-Breaks? That depends upon your application, but it's always better to play it safe.

The LoadJVM function (in te.c) queries the Windows registry for the location and name of the JVM DLL. This information is located in a subkey of the HKEY_LOCAL_MACHINE root key. This subkey is SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.2 and contains a RuntimeLib entry. The value associated with this entry is the location and name of the JVM DLL.

Finally, let's examine te.java. Figure 2 shows te's main window.

Figure 2. Main window of te

This window consists of a single File menu (with Load, Save, and Quit options), a text area for viewing and editing the contents of a file, and a pair of status lines (one line identifies currently loaded or saved file while the other line displays error messages).

Before concluding, let's summarize what you need to do in order to run the text editor on your computer.

  • Install the JRE
  • Place te.exe and te.class in the same directory
  • Set the CLASSPATH environment variable (a Win32 environment variable that the JVM uses to locate class files) to point to the directory containing te.class


Caution
In April 2006, I received a question from a JavaWorld reader who was unable to run te.exe and see the te GUI. I discovered that this problem exists because of a hard-coded registry path in the te.c source code. Back in 1999, when I wrote te.c, J2SE 1.2 was current and I hardcoded its 1.2 version number as part of the following registry path: SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.2. Because subsequent versions of Java place different version numbers below the Java Runtime Environment subkey, this path no longer works. For example, J2SE 1.4 places 1.4 and 1.4.0 entries below Java Runtime Environment. To make a te.exe that works in that environment, you would replace that path's 1.2 with either 1.4 or 1.4.0, as shown in the following code fragment (from te.c's LoadJVM() function), and recompile:
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
                  "SOFTWARE\\JavaSoft\\Java Runtime Environment\\1.4",
                  0,
                  KEY_ALL_ACCESS,
                  &key) != ERROR_SUCCESS)
In hindsight, I should have come up with a better way of making te.exe portable to various versions of Java.


While the Win32-Java hybrid approach won't be useful in all situations, it's a good little hack to keep your Java skills relevant and your Java code running in a Windows environment. With a little practice, this alternative method can become a valuable tactic for getting through the turbulence that now has the Java community rattled with uncertainty.

About the author

Jeff Friesen, a computer consultant and software developer, works with a wide variety of software technologies, including C++, digital signatures/encryption, Java, smart cards, and Win32. He also writes Java articles for JavaWorld and C++ articles for PC Magazine.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources