A quick way to create an installer for a Java program

You never had to quickly create an installer for your Java application, but didn’t want to spend a lot of time on it creating your own? You might be surprised, but such a tool is already included in the standard JDK7 package.

Short description


javafxpackager is a utility created to create packages from programs written using JavaFX. Some time after the creation, Oracle decided that the same utility can create packages for programs written purely in Java. They decided not to change the name.
What is served at the entrance? You can input both the directory with the already compiled sources and the already built jar. Then the jar is packaged with the JRE and you can submit this to the user with java not yet installed. This allows you to not force the user to install the JRE themselves. So how to use it?

Examples of using


Suppose we have some kind of HelloWorld project: just the src directory with the helloworld subdirectory and HelloWorld.java in it:
package helloworld;
public class HelloWorld {
    public static void main (String[] args) {
        System.out.println ("Hello, world!");
    }
}

At the root lies the simplest ant'ovy build.xml, which can only collect class files:

So, let's just assemble the project:
$ ant compile

Using javafxpackager you can collect jars, which we will do (after creating the dist directory):
$ javafxpackager -createjar -srcdir build/classes -outfile dist/HelloWorld.jar -appclass helloworld.HelloWorld

If you look at the contents of the created jar, we will see that it is somewhat different from the standard created:
$ unzip -l dist/HelloWorld.jar 
Archive:  dist/HelloWorld.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2013-04-29 15:50   META-INF/
      158  2013-04-29 15:50   META-INF/MANIFEST.MF
        0  2013-04-29 15:50   helloworld/
      353  2013-04-29 15:50   helloworld/HelloWorld.class
        0  2013-04-29 15:50   com/
        0  2013-04-29 15:50   com/javafx/
        0  2013-04-29 15:50   com/javafx/main/
     2671  2013-04-29 15:50   com/javafx/main/Main$1.class
     5633  2013-04-29 15:50   com/javafx/main/NoJavaFXFallback.class
    19218  2013-04-29 15:50   com/javafx/main/Main.class
     1747  2013-04-29 15:50   com/javafx/main/Main$2.class
---------                     -------
    29780                     11 files

After unpacking it and opening MANIFEST.MF, we will also see that the inclusion method is also slightly different from the standard one (it is typical for JavaFX):
Manifest-Version: 1.0
JavaFX-Version: 2.2
JavaFX-Application-Class: helloworld.HelloWorld
Created-By: JavaFX Packager
Main-Class: com/javafx/main/Main

Next, let's move on to creating packages ourselves. By default, the deploy directive only creates jnlp and html with a built-in plugin.
But if you specify the -native all directive, then a package is created that is specific to this operating system: deb and rpm for Linux (any containing dpkg or rpmbuild for each package, respectively), exe and msi for Windows (unfortunately, there is a limitation and the following utilities should be installed: Inno Setup to create exe and WiX Toolset for msi) and an app with dmg for MacOS X. Let's create native packages right away (we’ve already supposedly created jar):
$ javafxpackager -deploy -v -srcdir dist -outdir dist -outfile HelloWorld -appclass helloworld.HelloWorld -native all

After this, assembly will take place for some time. Now in our dist directory various bundles have appeared: 2 bundles (for different OSs - different) and 1, which is essentially an unpacked package: the HelloWorld directory, in which the HelloWorld binary is located, launching which we immediately get the result:
Hello world!

This code starts from the JRE directory already packaged.
And packages can already be installed.

But how to add such an assembly to the project?


There are 2 ways: you can use the same javafxpackager, adding it to your, for example, ant-scripts, using, for example, exec'a. But you can do much better. The java package also includes the ant-javafx.jar package, which allows you to add all of this to your ant scripts. What needs to be done for this?
  1. Add ant-javafx support: set the parameter in the root element of the project xml tree
    xmlns: fx = "javafx: com.sun.javafx.tools.ant"
    , and in his body add
    , while you must have set the variable $ {java.sdk} to the root of the JDK.
  2. jar:
  3. deploy:


  4. docs.oracle.com/javafx/2/deployment/javafxpackager001.htm - documentation on javafxpackager
    docs.oracle.com/javafx/2/deployment/javafx_ant_task_reference001.htm - documentation on antock tasks
    docs.oracle.com/javafx/2 /self-contained-packaging.htm - additional information

    UPD: All those that are specified as not working here work in modern versions of JDK.

Also popular now: