Create Windows Executable (.exe) for Java Application

Launch4j is a cross-platform tool for wrapping Java applications distributed as jars in lightweight Windows native executable files. In this post, we will learn making such an executable file for a demo java application.

Step1 ) Create a java application

I am creating a very basic java class which simply displays a frame and some text on it. It also has the main() method which will start the application.

package com.howtodoinjava;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;

public class ApplicationMain extends JDialog 
{
	private static final long serialVersionUID = 1L;
	public ApplicationMain() 
	{
		//Create a frame
		Frame f = new Frame();
		f.setSize(500, 300);
		
		//Prepare font
		Font font = new Font( "SansSerif", Font.PLAIN, 22 );
		
		//Write something
		Label label = new Label("Launch4j Maven Demo with HowToDoInJava.com");
		label.setForeground(Color.RED);
		label.setFont(font);
		f.add(label);

		//Make visible
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	public static void main(final String[] args) 
	{
		new ApplicationMain();
	}
}

Step 2) Create maven file with launch4j configured in it

I have created a pom file for packaging the application as .exe file. If you feel something unclear, drop a comment.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.howtodoinjava</groupId>
  <artifactId>JavaExeDemo</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>JavaExeDemo</name>
  <url>http://maven.apache.org</url>
  <properties>
	    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.7.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>shaded</shadedClassifierName>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.howtodoinjava.Main</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.akathist.maven.plugins.launch4j</groupId>
            <artifactId>launch4j-maven-plugin</artifactId>
            <version>1.5.1</version>
            <executions>
                <execution>
                    <id>l4j-clui</id>
                    <phase>package</phase>
                    <goals>
                        <goal>launch4j</goal>
                    </goals>
                    <configuration>
                        <headerType>gui</headerType>
                        <jar>${project.build.directory}/${artifactId}-${version}-shaded.jar</jar>
                        <outfile>${project.build.directory}/howtodoinjava.exe</outfile>
                        <downloadUrl>http://java.com/download</downloadUrl>
                        <classPath>
                            <mainClass>com.howtodoinjava.ApplicationMain</mainClass>
                            <preCp>anything</preCp>
                        </classPath>
                        <icon>application.ico</icon>
                        <jre>
                            <minVersion>1.6.0</minVersion>
                            <jdkPreference>preferJre</jdkPreference>
                        </jre>
                        <versionInfo>
                            <fileVersion>1.0.0.0</fileVersion>
                            <txtFileVersion>${project.version}</txtFileVersion>
                            <fileDescription>${project.name}</fileDescription>
                            <copyright>2012 howtodoinjava.com</copyright>
                            <productVersion>1.0.0.0</productVersion>
                            <txtProductVersion>1.0.0.0</txtProductVersion>
                            <productName>${project.name}</productName>
                            <companyName>howtodoinjava.com</companyName>
                            <internalName>howtodoinjava</internalName>
                            <originalFilename>howtodoinjava.exe</originalFilename>
                        </versionInfo>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

Step 3) Create .exe file

To make the exe file for above java program, run maven command:

mvn package

Above command will create the “howtodoinjava.exe ” file in your project’s target folder. Double click on .exe file will open the window like this.

Launch4j maven demo to create java executable
Launch4j maven demo to create java executable

 

If you want to download source code of above application, click on below given download link.

Sourcecode Download

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

21 thoughts on “Create Windows Executable (.exe) for Java Application”

  1. Hi Lokesh,

    I have 3 three questions:

    1) Under how to give relative path because if i just give img.ico it wont find i have placed icon under icon folder how can i specify relative path to image apart from hard coding path name like C:\… Since it wont work in others system.

    2) Where we can see error log generated?

    3) I have created Eclipse Plugin Project and given main class as packagename.Activator getting error as
    “A Java Exception has occured” to report full error log i need answer for 2nd question.

    Thanks,
    Vinod

    Reply
  2. There is a small mistake in the pom.xml file:

    <mainClass>com.howtodoinjava.Main</mainClass>
    

    should be replaced with

    <mainClass>com.howtodoinjava.ApplicationMain</mainClass>
    
    Reply
  3. Hi,
    I’m developing the desktop application in java 7 , 3 classes are there in my application. i created the java .exe file by using launch 4j but it is showing error as ” java exception has occured”

    Reply
  4. Its working now…

    I am able to make my jar as exe…
    But I have new issue.

    My Jar was reading config properties(kept outside of jar)… how to do it in exe…..where to set classpath in exe so that it can read config.properties

    Reply
  5. Failing because of :-
    net.sf.launch4j.BuilderException: Application jar doesn’t exist.
    at net.sf.launch4j.Builder.build(Builder.java:77)

    Reply
  6. I executed your example without any issue….but when I am trying to use same approach for my project code….then getting following error (Any Idea why failing…):

    [INFO] Platform-specific work directory already exists: C:\Users\vikas\.m2\repo
    sitory\com\akathist\maven\plugins\launch4j\launch4j-maven-plugin\1.5.1
    [ERROR]
    net.sf.launch4j.BuilderException: Application jar doesn’t exist.
    at net.sf.launch4j.Builder.build(Builder.java:77)
    at com.akathist.maven.plugins.launch4j.Launch4jMojo.execute(Launch4jMojo
    .java:353)
    at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPlugi
    nManager.java:490)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(Defa
    ultLifecycleExecutor.java:694)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLi
    fecycle(DefaultLifecycleExecutor.java:556)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(Defau
    ltLifecycleExecutor.java:535)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHan
    dleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmen
    ts(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLi
    fecycleExecutor.java:180)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:6
    0)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

    Reply
  7. I just copied above code and rant it, but i am getting below error
    [ERROR] Failed to execute goal com.akathist.maven.plugins.launch4j:launch4j-mave
    n-plugin:1.5.1:launch4j (l4j-clui) on project JavaExeDemo: Failed to build the e
    xecutable; please verify your configuration. Icon doesn’t exist. -> [Help 1]

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.