DevNation
DevNation

DevNation

How to create executable JAR

DevNation's photo
DevNation
·Aug 7, 2021·

2 min read

Intro:-

Jar files (Java ARchive files) is a collection of multiple Java class files. A jar is an archiving format that stores directories and source files. And can be run as an executable file.A JAR (Java ARchive) is a way of packaging together all of the resources associated with a program (class files, images, sounds, etc.). Jar files can be distributed as a single executable file, which save disk space and ease download process.

Task:-

You are packaging and executing a spring boot app in to JAR

Software Requirements:-

Ubuntu-20.04 +jit [linux-x86_64]
Open Jdk 8
Spring Boot 2.2

Level:-

Beginner

Prerequisite:-

Knowledge of core java and text editor

Step 1:-

Using Spring Initializr(start.spring.io) download starter app by filling required parameters. Unzip the folder at your computer.

Step 2:-

Add JAR in your pom.xml

<packaging>jar</packaging>

jarpom.png
Make sure your pom.xml also contain spring boot plugin

<plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>

pluginpom.png

Step 3:-

Run command: $ mvn clean package from terminal

Step 4:-

previous command will make target folder, in which contains two JAR

a. runner-0.0.1-SNAPSHOT.jar : This is executable JAR
b. runner-0.0.1-SNAPSHOT.jar.original : This is original JAR and not executable.

Step 5:-

Suppose you have following pom.xml file in your spring boot project

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.devnation</groupId>
    <artifactId>runner</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>runner</name>
    <description>Demo project for runner</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Step 6:-

Run executable JAR as follows
$ java -jar target/runner-0.0.1-SNAPSHOT.jar

jarrunner.png Server started.

Conclusion:-

After performing above steps you made a spring boot nano app,which package and execute JAR and by making these app you get acquainted with spring boot basics about packaging and executing JAR .

 
Share this