Portable GUIs Improve Application Flexibility

by John Sharp, Content Master Ltd.


Developers can run Java* language GUIs in Microsoft .NET* if they take a few guidelines into account ahead of time

Microsoft Visual J# .NET* includes an implementation of many Java Development Kit (JDK) 1.1.4* packages, including the Abstract Windowing Toolkit (AWT)*, which is used to create Graphical User Interfaces (GUIs) with the Java programming language. This functionality makes it possible for developers to build GUIs that can run unchanged on platforms such as Unix* (including Solaris* and Linux*), as well as on Windows*.

Developers can create GUI applications using the AWT that will run unchanged using Java running on Unix and Visual J# .NET using the Microsoft Common Language Runtime (CLR)* running on Windows. This paper concentrates on using the J# compiler, Vjc.exe, to convert working sets of Java language source files into .NET executables and .NET assemblies.

Readers should note that an alternative way to integrate Java code into .NET is to translate Java byte code such as that held in .class files into .NET Common Intermediate Language (CIL)* code using the JbImp.exe command line utility (also included with Visual J# .NET). Using JbImp is covered in a separate white paper titled "Integrating Java and Microsoft .NET."

A second possible alternative is to use the Microsoft Java Language Conversion Assistant* to translate Java language code into C#.

The Microsoft .NET Framework* is not without limitations with regard to Java functionality. For instance, Java Swing* packages cannot be used with J#, as they became available after JDK 1.1.4. Java GUI applications that employ Swing must be manually recoded to use the equivalent Windows Forms* constructs of the .NET Framework. J# also does not provide support for Java Remote Method Invocation (RMI)*, the Java Native Interface (JNI)*, Java applets*, or the ability to dynamically load Java classes from bytecode (.class) files.

Graphical applications that use these features must be manually converted to use the appropriate .NET mechanisms. Further discussion of these issues is beyond the scope of this paper.

A Sample Java GUI Application

The code listings that accompany this article show the main elements of a basic GUI application written in the Java language. The application displays a window (EmployeeFrame) that allows the user to enter employee details for an organization (the application does not save those details anywhere, as the purpose of this example is simply to show the user interface).

It is not necessary to examine every statement in this program; simply note that Listing 1, the EmployeeFrame class, is an AWT Frame that contains a number of common AWT components, such as labels, text boxes, radio buttons, and a drop-down list box. The frame also uses a menu. The standard AWT mechanisms are used to intercept and handle events such as menu items being clicked and the frame being closed. The features and constructs the frame employs are typical of those used by any AWT application.

Listing 2, the AboutDialog class, is a frame containing very limited help information. It is invoked from the Help | About item in the EmployeeFrame main menu bar. Listing 3, the QuitDialog class, displays a confirmation message when the user exits the application.

If you have the JDK installed on your computer, you can use the following commands to compile and run this simple Java example program:

javac EmployeeFrame.java AboutDialog.java QuitDialog.java

java EmployeeFrame


Figure 1 shows the application running under Solaris (as an example of a Unix platform). The user has entered some details and selected Exit from the File menu:

Figure 1. The Employee application running under Solaris.

Figure 2 shows the same application, compiled and executed using the same commands, running under Windows:

Figure 2. The Employee application running under Windows.

Compiling .java Files Using Vjc.exe

As these Java source files rely only on JDK 1.1.4 packages, you can use the Visual J# .NET compiler (Vjc.exe) to compile them into a .NET executable:

vjc /target:exe /out:EmployeeFrame.exe EmployeeFrame.java AboutDialog.java QuitDialog.java


These commands generate an executable program called EmployeeFrame.exe. You can run this program like any other Microsoft .NET application:



Figure 3 shows the result:

Figure 3. The .NE T version of the application running.
The .exe file created is a .NET executable assembly, a self-describing CIL program that is just-in-time compiled into a native executable by the .NET Common Language Runtime (CLR)* when run. You can inspect the internal structure of a .NET executable using a tool such as IlDasm.exe:

ildasm EmployeeFrame.exe


Figure 4 illustrates the EmployeeFrame.exe program in IL DASM. You can expand the various classes to display the details of their methods and fields (Figure 4 shows the contents of the QuitDialog class):

Figure 4. IL DASM showing the EmployeeFrame executable assembly

From this you can see that Vjc preserves the names and types used in the original Java program. Depending on how familiar you are with the .NET assemblies that form the Visual J# .NET runtime, you will also notice that the JDK 1.1.4-compliant AWT classes themselves are implemented by the assembly VJSLIB.dll that is supplied with Visual J# .NET.

The only item displayed that is not part of the original Java program is the Manifest at the top of the window. The manifest contains the metadata that makes the executable self-describing. You can use IL DASM to inspect the manifest contents, as shown in Figure 5 (double-click on the word MANIFEST). For more information about the structure and contents of a .NET assembly, consult the documentation supplied with the Microsoft .NET Framework.

Figure 5. The manifest of the EmployeeFrame assembly.

This figure reveals that EmployeeFrame.exe depends on three other assemblies: MSCORLIB.DLL (core .NET functionality), VJSCORE.DLL, and VJSLIB.DLL (Java language and library functionality, installed with J#).

Translating .java Files Into a .NET Assembly Using Vjc.exe

You can use Vjc.exe to translate working sets of .java files into .NET library assemblies. Library assemblies can be shared by multiple .NET applications and called from other languages executing using the CLR. Specify the /target:library option to create a library assembly:

vjc /target:library /out:DotNetGui.dll GuiApplication.java


Library assemblies can be loaded and used by other .NET applications, regardless of the language used to build those applications. For more information, see the paper "Integrating Java and Microsoft .NET."


The .NET architecture has good tool support for translating Java GUI applications that use JDK 1.1.4 packages such as AWT into the equivalent .NET GUI applications. Visual J# .NET supplies its own implementations of many JDK 1.1.4 packages in the VJSLIB.DLL assembly. Once the .java source files are converted into .NET assemblies, they can be reused by any .NET program written in any .NET-supported language.

Additional Resources

J#, a Microsoft tool, provides tools to convert Java language source code into CIL (jvc.exe), and Java byte code (.class files) into CIL (jbimp.exe). Microsoft Visual J# is fully integrated with Microsoft Visual Studio 2003*. J# can also be downloaded separately from http://msdn2.microsoft.com/en-us/vjsharp/bb188594.aspx*.

.NET Framework SDK contains many tools, including the command line C# compiler (csc.exe), the CIL Disassembler tool (IlDasm.exe), and the .NET Framework libraries. A free download is available at http://msdn2.microsoft.com/en-us/netframework/aa569263.aspx*.

Listing 1: the EmployeeFrame class

Listing 2: the AboutDialog class

Listing 3: the QuitDialog class

Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.