Home

Awesome

Launch5j

Java JAR file wrapper for creating Windows™ native executables

Introduction

Launch5j is a reimagination of “Launch4j”, with full Unicode support. This is a tool for wrapping Java applications distributed as JARs in lightweight Windows™ native executables. The executable can be configured to search for a certain JRE version or use a bundled one. The wrapper also provides better user experience through an application icon, a native pre-JRE splash screen, and a Java download page in case the appropriate JRE cannot be found.

Usage

There currently are two different ways to use Launch5j with your application code:

Variants

Launch5j executables come in a number of variants, allowing you to pick the most suitable one for your project:

Supported platforms

All of the above Launch5j variants are available as x86 (32-Bit) and amd64 (64-Bit) executables. The x86 (32-Bit) executables can run on 32-Bit and 64-Bit versions of Microsoft® Windows™, whereas the amd64 (64-Bit) executables require a 64-Bit version of Microsoft® Windows™. Consequently, it is generally recommended to distribute the x86 (32-Bit) launcher executable. Please note that this does not restrict the “bitness” of the JRE that can be used. Even the x86 (32-Bit) launcher executable is perfectly able to detect and launch a 64-Bit JRE – if it is available.

Note: Launch5j has been tested to work correctly on Windows XP with Service Pack 3, or a compatible newer version.

Customizations

Launch5j comes with a default executable icon and a default splash screen bitmap. These just server as an example and you probably want to replace them with your own application-specific graphics.

It is not necessary to re-build the executable files for that purpose. Instead, you can simply use a resource editor, such as XN Resource Editor (mirror) or Resource Hacker™, to modify the pre-compiled executable files as needed:

Resource Hacker

Additional options

Some options can be configured via the launcher executable's STRINGTABLE resource:

Note: We use the convention that the default resource string value "?" is used to represent an “undefined” value, because resource strings cannot be empty. You can replace the default value as needed!

Unicode command-line arguments

There is a long-standing bug in Java on the Windows™ platform, which causes Unicode command-line arguments to be “mangled”. Even if the Unicode command-line arguments are properly passed to the Java executable (java.exe), they are not passed trough correctly to the main() method of your Java program! This problem can be reproduced in all Java versions ranging from Java 7 (1.7) up to and including the latest Java 15, as of October 2020.

More specifically, Java replaces any characters in the given command-line arguments that can not be represented in the computer's local ANSI codepage (i.e. pretty much any non-ASCII characters) with ? characters. The cause of the problem apparently is that the “native” C code of the Java executable still uses the legacy main() entry point instead of the wmain() entry point; the latter is the modern Unicode-aware entry point – which should be used by programs written for Windows 2000 or later. Why this has not been fixed in 20 years is beyond my understanding.

As a workaround for the current situation in Java, Launch5j will (by default) convert the given Unicode command-line arguments to the UTF-8 format and then apply the URL encoding scheme. This ensures that only pure ASCII characters need to be passed to the Java executable, thus preventing the command-line arguments from being “mangled”. Still the original Unicode command-line arguments are preserved and can be reconstructed in the Java code.

The only downside is that a bit of additional processing is required in the application code. The command-line arguments can be decoded by using the URLDecoder.decode() method with the UTF-8 charset. Also, applications should check the l5j.encargs system property before decoding the command-line arguments:

public class MainClass {
    public static void main(final String[] args) {
        initCommandlineArgs(args);
        /* Your application code goes here! */
    }

    private static void initCommandlineArgs(final String[] argv) {
        if (boolify(System.getProperty("l5j.encargs"))) {
            final String enc = StandardCharsets.UTF_8.name();
            for (int i = 0; i < argv.length; ++i) {
                try {
                    argv[i] = URLDecoder.decode(argv[i], enc);
                } catch (Exception e) { }
            }
        }
    }

    /* ... */
}

Example:

Unicode command-line arguments

Please refer to the file example/example.java for the complete example code!

JAR file name

Be aware that the same problem of “mangled” Unicode characters applies when the path of the JAR file is passed to the Java executable. In other words, the Java runtime fails to execute any JAR files whose path – file name or any directory name in the path – contains any Unicode characters that cannot be represented in the computer's local ANSI codepage! Unfortunately, we can not encode the path of the JAR file as we do with the other command-line arguments, because the Java executable requires the path of the JAR file to be passed in a non-encoded form.

Therefore, it is recommended to only use ASCII characters in the name of your JAR file and in the “install” path !!!

Command-line options

The launcher executable recognizes the following “special” command-line options:

Downloads

Official Launch5j download mirrors:

It is highly recommended to download Launch5j only from one of the mirrors listed above. We are not responsible for the integrity and trustworthiness of Launch5j downloads you may have received from other locations!

Source code

Launch5j source code is available via Git:

Build instructions

In order to build Launch5j from the sources, it is recommended to use the GNU C Compiler (GCC) for Windows™, as provided by the Mingw-w64 project. Other C compilers may work, but are not officially supported.

Probably the most simple way to set up the required build environment is by installing the MSYS2 distribution, which includes GCC (Mingw-w64) as well as all the required build tools, such as Bash, GNU make and Git.

Please make sure that the essential development tools and the MinGW-w64 toolchains are installed:

$ pacman -S base-devel git
$ pacman -S mingw-w64-i686-toolchain mingw-w64-x86_64-toolchain

Once the build environment has been set up, just run the provided Makefile:

$ cd /path/to/launch5j
$ make

Note: In order to create 32-Bit or 64-Bit binaries, use the mingw32 or mingw64 sub-system of MSYS2, respectively.

Contact

Launch5j was created by LoRd_MuldeR <mulder2@gmx.de>.

For help and support, please visit:
https://github.com/lordmulder/Launch5j/issues

Acknowledgment

This project is partly inspired by the “Launch4j” project, even though it has been re-written from scratch:
https://sourceforge.net/p/launch4j/

License

This work has been released under the MIT license:

Copyright 2024 LoRd_MuldeR <mulder2@gmx.de>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.