How to create an exe for a java program using maven?

3 comments
Usually a software is distributed as an executable file. Therefore as programmers we would need to create an .exe file for our programs. Through this post I will present you how to achieve it easily with Maven (a build automation tool used primarily for Java projects) .

A windows executable can be created by using a combination of two maven plug-ins , Maven Shade plugin and launch4j plugin. 

Here is my program. It is to read an .xml file and write its content to the standard out put. In order to read the .xml file we have to use a library. Here I have used jdom2. Like that in developing software we have to depend on many libraries in order to prevent reinventing the wheel. If we are building our projects using maven we can include those dependencies in the pom.xml file


import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class Envision {
    public static void main(String[] args){
        File xmlFile = new File("D:\\example.xml");
        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List books = rootNode.getChildren("book");

            System.out.println("This is my book store");

            for (int l = 0; l < books.size(); l++) {

                Element book = (Element) books.get(l);
                System.out.println("Name :"+book.getChildText("name")+"     Author :"+book.getChildText("author"));
                System.out.println("----------------------------------------------------------------------");

            }



        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }
    }
}

Here is my pom.xml file. Here Maven Shade plugin is used to add all the dependencies in the program into the runnable jar file. The launch4j creates the .exe with vender information and a nice icon too. 

I have added the exe.ico in src/main/resources. 



<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>Blog</groupId>
    <artifactId>Blog</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom2</artifactId>
            <version>0.0.6-BETA</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            <version>0.0.20131108.vaadin1</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            <version>0.0.20131108.vaadin1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <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>Envision</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>1.5.1</version>
                <executions>

                    <!-- Command-line exe -->
                    <execution>
                        <id>l4j-cli</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>console</headerType>
                            <outfile>target/envision.exe</outfile>
                            <jar>target/${artifactId}-${version}.jar</jar>
                            <errTitle>App Err</errTitle>
                            <classPath>
                                <mainClass>Envision</mainClass>
                            </classPath>
                            <icon>src/main/resources/exe.ico</icon>
                            <jre>
                                <minVersion>1.5.0</minVersion>
                            </jre>
                            <versionInfo>
                               <fileVersion>1.0.0.0</fileVersion>
                               <txtFileVersion>${project.version}</txtFileVersion>
                               <fileDescription>${project.name}</fileDescription>
                               <copyright>2014 envision.com</copyright>
                               <productVersion>1.0.0.0</productVersion>
                               <txtProductVersion>1.0.0.0</txtProductVersion>
                               <productName>${project.name}</productName>
                               <companyName>Envision</companyName>
                               <internalName>envision</internalName>
                               <originalFilename>envision.exe</originalFilename>
                            </versionInfo>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>


</project>

After configuring the pom.xml file just execute maven install to get the .exe file. 

That's it. Check the target folder in you project folder to find the .exe. 

Hope this would help you :) 

Happy Coding! 

3 comments :

  1. Nice post :-)

    If the application has a GUI which is built from the GUI tool of IntelliJ IDEA, you need to have "ideauidesigner-maven-plugin" in the pom. Following link describes it in detail including packaging a jar along with the dependencies as well.

    http://glxn.net/2010/08/17/making-a-swing-project-using-intellij-idea-and-gui-builder-with-maven-including-executable-jar/

    Thought it is worth make a note :-)

    ReplyDelete
  2. Play Slots, Live Dealer and Table Games at a Casino - Dr. McD
    From classic to 춘천 출장샵 innovative, our guests 서산 출장샵 can now play the top slots, 여수 출장안마 table 광명 출장안마 games and 강릉 출장샵 live dealer games at the casino.

    ReplyDelete