Search code examples
javajava-8windows-installer

Installing Java executables on Windows 10 machines by example


I imagine some Java gurus with experience delivering Java apps on Windows desktops will be able to ace this one. I've always been a Mac/Linux Java developer so this is uncharted territory for me :-/.

I have to write a Java 8 Swing application and install it on a Windows 10 (64-bit) machine. My gameplan is to package the app as an executable JAR and wrap it with Launch4J, so that it looks like a native Windows EXE (.exe file). However its a little bit more complication than that when it comes to the distribution:

  • There will be the JAR/EXE as mentioned above, lets call it myapp.exe (built from myapp.jar)
  • The app will output logs to a (local?) directory, myapp.log
  • The app will load a config file at runtime, myapp.properties
  • The distribution should also contain the User's Guide, MyApp User Guide.html

Let's assume a Java 8 JRE/JDK is already installed on the machine, so we don't need to worry about installing Java itself.

The installation process must be simple and include:

  1. Removing the old version (and all of its other artifacts such as the log file, config/properties file, user guide, etc) off the machine completely
  2. Installing the new version at either the Windows 10 default location, or allowing the user to specify a different location

Additionally, if at all possible, I'd like the installation process to include:

  1. A requirements check for things like minimum memory and disk space, OS version info/compatibility (i.e. make sure its being installed on Windows 10, etc.)
  2. Provide an easy-to-use wizard such as an MSI that the user can click though
  3. Optionally install shortcuts to the user's Desktop

Given all this, I'm wondering what my options are in the modern Windows 10/Java/Launch4J landscape. Are there tools that will help me script together MSIs quickly, or do I have to write my own in, say, C#/.NET and have that be a separate binary/project? If MSIs aren't an option, what options exist that might hit all my bullets above?

I realize I could just distribute the whole thing as a ZIP, and have the installation process look something like:

  1. Save the ZIP to some place on the user's machine, say, the Desktop
  2. Move the previous app and its artifacts to the trash, manually
  3. Unzip the new ZIP

However that feels janky and I'm looking for something more professional. Any solutions here?


Solution

  • JDK 8 is bundled with a tool called javapackager (formerly javafxpackager) which is part of JavaFX. However, you can use it package java swing application without using JavaFX. This tool can generate an installer file (exe or msi) which contains the application and the Java runtime as well.

    Here is an example:

    javapackager -deploy -native exe -Bruntime="C:\Program Files\Java\jdk1.8.0_66\jre" -Bicon=app_icon.ico -BsystemWide=true -BshortcutHint=true -outdir packages -outfile appFile -srcdir dist -srcfiles MyApp.jar;COPYING.txt -appclass somePackage.MainClass -BlicenseFile=COPYING.txt -name appName -title "The application name"

    For more information, see adding icon to bundle using javapackager


    There is also a new tool called jpackage which is based on javapackager. It is proposed to be released with the next JDK release, JDK 14. Note that javapackager was removed from JDK since version 11 as part of the removal of JavaFX.

    See A Brief Example Using the Early Access jpackage Utility