Generating a console 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 console executable. The Java program that we are going to create an executable for is a simple "Hello, World" program. This program is called Console.java, and can be found in the examples-source\ subdirectory.

File: Console.java
1:  package examples;
2:
3:  public class Console
4:  {
5:      public static void main(String[] args) throws Exception
6:      {
7:          if (args.length == 0)
8:              System.out.println("Hello World!");
9:          else
10:             System.out.println("Hello " + args[0] + "!");
11:     }
12: }

This simple program prints out "Hello, World!" when run without any parameters.

C:\Program Files\NativeJ\examples> java -cp examples.jar examples.Console
Hello, World!

However, when supplied with a name as a parameter, it will print out "Hello, <name>!".

C:\Program Files\NativeJ\examples> java -cp examples.jar examples.Console John
Hello, John!

Let's see how we can create an executable called console.exe that will run the examples.Console program.

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

Run NativeJ. Then, click on File... Open... and open examples\console.njp. This is the project file for examples.Console, and it has the following parameters:

Name Value Remarks
Main
Application type
Console
The executable generated will run as a console program.
Executable filename (32-bit)
console.exe
The filename of the generated executable.
Executable icon
console.ico
The application icon.
Classpath
Classpath
examples.jar
This contains required class files i.e. examples.Console</>.
Application
Main application class
examples.Console
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 console.exe has been generated.

Run console.exe without any parameters:

C:\Program Files\NativeJ\examples> console
Hello, World!

Now, run console.exe with a name:

C:\Program Files\NativeJ\examples> console John
Hello, John!

You can also supply some default arguments to examples.Console. Enter John in Default arguments, then generate the executable again. Now, run console.exe without any parameters:

C:\Program Files\NativeJ\examples> console
Hello, John!

Run console.exe with another name besides John:

C:\Program Files\NativeJ\examples> console Mary
Hello, Mary!

As you can see, the executable will use the default arguments if none are available. If the user supplies the arguments, then these are used instead of the default.

In this tutorial, the JVM DLL parameter is left blank so that when console.exe is run, it will automatically detect and load a suitable JVM (in this case, any JVM version >= 1.2) on the target system. You can also bundle a specific JVM with your application and force the executable to use that JVM. To find out how, please refer to Bundling a Particular Java Runtime.