Core Java

Native Java Packaging with NetBeans 7.4

One of the new features of NetBeans 7.4 that made the “NetBeans 74 NewAndNoteworthy” page is “Native Packaging,” which is described on that page as “JavaSE projects now support creation of native bundles taking use of the native packaging technology provided by JavaFX.”

I will use a very simple example to demonstrate this native packaging functionality in NetBeans 7.4. The next code listing is for this enhanced Hello World example.
 
 
 
 
EnhancedHelloWorld.java

package dustin.examples;

import static java.lang.System.out;

/**
 * Slightly enhanced "Hello World" example.
 * 
 * @author Dustin
 */
public class EnhancedHelloWorld
{
   /**
    * Main function.
    * 
    * @param args the command line arguments; name being addressed, if any.
    */
   public static void main(String[] args)
   {
      final String addresseeName = args.length > 0 ? args[0] : "World";
      out.println("Hello, " + addresseeName);
   }
}

The next image shows this same code in the NetBeans 7.4 source code editor.

enhancedHelloWorldCodeInNetBeans74

To use the Native Packaging feature, I can right-click on the project and select Properties as shown in the next image.

rightClickNb74ProjectSelectProperties

Clicking on “Properties” leads to the appearance of the “Project Properties” window. This window, as shown in the next screen snapshot, allows the developer to expand the “Build”, select “Deployment”, and check the box next to the label “Enable Native Packaging Actions in Project Menu.” Selecting this option configures NetBeans 7.4 to support native packaging for that NetBeans project.

projectPropertiesNetBeans74

With NetBeans 7.4 native packaging enabled, I can now right-click on the project and have a new option called “Package as” available. When I select that “Package as” option, I see the following choices: “All Artifacts”, “All Installers”, “Image Only”, “EXE Installer”, and “MSI Installer”. Note that my NetBeans 7.4 IDE is running on a Windows machine, so the EXE and MSI installers make sense. Sections 6.4.1 and 6.4.2 of the Deploying JavaFX Applications document cover the EXE and MSI installer packages respectively.

When I select EXE as the installer package, I see it processing the native packaging per the message in the bottom right corner of the IDE. This is shown in the next screen snapshot.

netbeans74BuildingNativeExeHelloWorld

The first time I tried this, I ran into an error reported by NetBeans with the message: “JavaFX native packager requires external Inno Setup 5+ tools installed and included on PATH to create EXE installer. See http://www.jrsoftware.org/”. Going to the referenced Jordan Russell Software site allows me to download Inno Setup 5.5.4 (isetup-5.5.4.exe). In my case, I downloaded the self-extracting EXE and ran it. I then added the full path to the directory into which Inno Setup 5.5.4 was installed to my PATH environmental variable and restarted NetBeans 7.4.

With Inno Setup installed on my system, the Inno Setup 5.5.4 installer compiler runs when NetBeans’s EXE native packaging is selected. When NetBeans and Inno Setup complete, a relatively large EXE file exists in the project’s directory as shown in the next screen snapshot.

dir_b_showsNetBeans74AppWithInno5Setup

I can run this executable, of course, by simply typing its name at the command prompt. The next screen snapshot demonstrates that running this executable leads to a popup window requesting approval to install the Java application.

enhancedHelloWorldInnoSetupReadyToInstall

When the “Install” button is clicked, the installation begins and this is demonstrated in the next screen snapshot.

SetupEnhancedHelloWorldInstalling

The executable installer installs the Java application as another executable. In this case, this application is installed in C:\Users\Dustin\AppData\Local\EnhancedHelloWorld as shown in the next screen snapshot.

generatedEnhancedHelloWorldApplication

The generated directory shown in the screen snapshot above includes a “runtime” directory with the necessary JRE to run this application even on machines that don’t have a JRE installed. The Java application itself is stored as a JAR in the “app” directory. Both of these subdirectories are shown in the next two screen snapshots.

generatedEnhancedHelloWorldApplicationJreDirectory

generatedEnhancedHelloWorldApplicationAppDirectory

The generated directory includes two .exe files. One is EnhancedHelloWorld.exe, which is the Java application executable. The other .exe file is unins000.exe. Running this latter .exe file cleanly uninstalls the application from the computer.

The next screen snapshot shows that I am able to start the application from my Window Start in addition to clicking on the generated executable file.

enhancedHelloWorldFromWindowsStart

Although the Java code sample I started with can be built as an executable application using NetBeans 7.4 as shown in this post, it is far more interesting to use a Java application with a user interface. For example, one could build an executable application with NetBeans 7.4 based on the Java class HelloWorldSwing.

My examples in this post have been entirely Java SE (no JavaFX), but have taken advantage of NetBeans 7.4’s support of native packaging via mechanisms generated for JavaFX deployments. Therefore it’s not surprising that the JavaFX documentation on Self-Contained Application Packaging is useful for understanding the options available. Native packaging with NetBeans 7.4 is also demonstrated in Native Packaging in NetBeans IDE.
 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button