Generating a graphical executable

Note: The following tutorial assumes you have the Java Runtime Environment (1.2 and above) installed on your machine.

In this tutorial, we are going to learn how to generate a graphical executable i.e. a native executable that behaves like a Win32 GUI program.

The Java program that we are going to create an executable for is a simple "Hello, World" type AWT program. This program is called Gui.java, and can be found in the examples-source\ subdirectory.

File: Gui.java
1:  package examples;
2:
3:  import java.awt.*;
4:  import java.awt.event.*;
5:
6:  public class Gui
7:  {
8:      public static void main(String[] args)
9:      {
10:         // Create the main window and components used by this app
11:         Frame frame = new Frame("Gui");
12:         String msg = "Hello World!";
13:         if (args.length > 0) msg = "Hello " + args[0] + "!";
14:         Label label = new Label(msg, Label.CENTER);
15:
16:         // Handle the exit event for the main window
17:         frame.addWindowListener(new WindowAdapter()
18:         {
19:             public void windowClosing(WindowEvent e)
20:             {
21:                 System.exit(0);
22:             }
23:         });
24:
25:         // Position the components within the main window
26:         frame.setLayout(new BorderLayout());
27:         frame.add(label, BorderLayout.CENTER);
28:
29:         // Resize and show main window
30:         frame.pack();
31:         frame.setSize(320, 240);
32:         frame.show();
33:     }
34: }

When run without any parameters, this program displays a "Hello, World!" message. When run with a name as the parameter eg. java examples.Gui John, it will display "Hello, <name>!" instead.

Let's see how we can create an executable called gui.exe, which will run the examples.Gui program.

The Gui.java file has been compiled and packaged in examples\examples.jar, along with the other examples. In this subdirectory, you will also find gui.njp, along with other .njp files.

Run NativeJ and open the gui.njp project. Confirm that it defines the following parameters:

Name Value Remarks
Executable
Application type
Graphical
The executable generated will run as a GUI program.
Executable filename (32-bit)
gui.exe
The filename of the generated executable.
Executable icon
gui.ico
The application icon.
Classpath
Classpath
examples.jar
This contains required class files i.e. examples.Gui</>.
Application
Main application class
examples.Gui
The name of the Java class we wish to run. This class should have a main() method.

Now, click on File... Generate... to generate the executable. If everything goes well, you will receive a notification that gui.exe has been generated.

Run gui.exe without any parameters, and with the name John. You should observe the same output as when running the program using java.

Now, run gui.exe twice without quitting. You should get the following error:

If you select Yes to Allow multiple instances and generate gui.exe again, you should be able to start multiple instances of gui.exe without any errors.