Launch4j - Generate exe from jar

by GarciaPL on Saturday 7 February 2015

Sometimes there is a some particular need that your program (of course mainly written in Java, that's obvious) must also be able to work under Windows environment (sic!). There is no such problem if you program will have still .jar extension, but recently business in my company demanded to create such program with .exe extension. I was convinced that it's not really possible. I was thinking how to mix different approaches to create applications under Windows and Linux environment.

But I finally found a very helpful and customizable tool called Launch4j [1]. This tool is mainly used by Graphical User Interface (GUI) where you define all configuration for instance Input (jar) and Output file (exe), some classpath, header, JRE version required by application to work, some custom environment variables, splash and of course information about version, copyright, product and company name etc.

Regarding to my case in company I must transform .jar to .exe file without using GUI. There was a need to create some REST interface which will receive some parameters like .jar file, .icon and other specified information to generate generic .exe file using of course launch4j.

To do so, you must include launch4j.jar to your project and use below Java code to generate .exe.

    public static void main(String[] args) {
        try {
            File file = new File("/home/lciesluk/configuration.xml");
            try {
                ConfigPersister.getInstance().load(file);
            } catch (ConfigPersisterException ex) {
                System.out.println("ConfigPersisterException " + ex.getMessage());
            }

            Config config = ConfigPersister.getInstance().getConfig();
            System.out.println("Input : " + config.getJar().getAbsolutePath());
            System.out.println("Output : " + config.getOutfile().getAbsolutePath());

            File baseDir = new File("/home/lciesluk/launch4j"); //extracted launch4j
            Builder b = new Builder(Log.getAntLog(), baseDir);
            File build = b.build();
            System.out.println("EXE location : " + build.getAbsolutePath());
        } catch (BuilderException ex) {
            System.out.println("BuilderException " + ex.getMessage());
        }
    }

I added also an additional sample of configuration file used by launch4j [2].
<launch4jconfig>
  <dontwrapjar>false</dontwrapjar>
  <headertype>gui</headertype>
  <jar>/home/lukasz/Motohurt.jar</jar>
  <outfile>/home/lukasz/Motohurt.exe</outfile>
  <errtitle>Please download and install Java</errtitle>
  <cmdline></cmdline>
  <chdir>.</chdir>
  <priority>normal</priority>
  <downloadurl>http://java.com/download</downloadurl>
  <supporturl></supporturl>
  <stayalive>false</stayalive>
  <restartoncrash>false</restartoncrash>
  <manifest></manifest>
  <icon>/home/lukasz/icon.ico</icon>
  <jre>
    <path></path>
    <bundledjre64bit>false</bundledjre64bit>
    <bundledjreasfallback>false</bundledjreasfallback>
    <minversion>1.7.0</minversion>
    <maxversion></maxversion>
    <jdkpreference>preferJre</jdkpreference>
    <runtimebits>64/32</runtimebits>
  </jre>
  <versioninfo>
    <fileversion>1.0.0.0</fileversion>
    <txtfileversion>1.0</txtfileversion>
    <filedescription>App</filedescription>
    <copyright>GarciaPL</copyright>
    <productversion>1.0.0.0</productversion>
    <txtproductversion>1.0</txtproductversion>
    <productname>App</productname>
    <companyname>GarciaPL</companyname>
    <internalname>GarciaPL</internalname>
    <originalfilename>App.exe</originalfilename>
  </versioninfo>
</launch4jconfig>

Reference : [1] Launch4j Home Page [2] Launch4j Configuration XML