EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Executable Jar
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot Executable Jar

Spring Boot Executable Jar

Introduction to Spring Boot Executable Jar

Spring boot executable jar is defined as a collection of class files that bundles the different classes written to accomplish the task the application is designed for. As a standard practice, when the application has more than a few dozen lines of code, the application is split into multiple classes, and while distributing the application, the classical format Java Archive is used for the purpose. Thus, spring boot Jars are self-contained Jar that has a collection of all the dependencies required for running the application and hence because of their size, is referred to as fat Jar.

Syntax of Spring Boot Executable Jar

The spring boot executable jar, as mentioned, contains the class files which incorporates the required utilities in order for the application to run and perform the designated task for which the application is built for. Here we will see about spring boot executable Jar from the syntax perspective so that while we see about how executable Jar is created and used along with all its options.

1. Building the Jar file using Maven.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

mvn clean install

2. Running the Jar file through the command line.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

java -jar <Name of the JAR file>

3. Running the Jar file through the command line and keep the process alive even after logging off.

nohup java -jar < Name of the JAR file >

4. Running the Jar file through the command line and keep the process alive even after logging off.

nohup java -jar < Name of the JAR file >

How to Create and Use an Executable Jar in Spring Boot?

Here we will see about how a Spring Boot application can be started from another program. In one word, we call it a methodology where an entire Spring Boot application is built into a single executable Jar archive and includes all the dependencies, packaged as nested Jars. The plugins in Maven does all the dirty work and provides you with the FAT Jar, which can be run easily using the command of java -jar.

Next, let us look at the way on how nested Jar works, as this sets the base for working and will be a critical component to keep in mind while we create and use an executable jar. Nested Jars are nothing, but Jars packed within the final runnable Jar file. The final runnable Jar file needs to contain the dependencies for the smooth and successful execution of the Jar file. For fulfilling the use case, shading is one of the options we have.

In this methodology, we include and rename the dependencies and then relocate the classes, thus rewrite the affected bytecode and the resources so that a copy is created, which is then bundled along with the main class of the Spring boot application. The classes and the resources are unpacked from dependencies and then repacked into a single runnable Jar. The concept of shading works for simple use cases, but in cases where 2 or more dependencies have the same resource file with the same name and path, it might create confusion, and hence some advanced techniques are used.

Different ways of creating an executable Jar in Spring Boot:

  • Manual Configuration: Using manual configuration, we need to include the maven-dependency plugin in the pom.xml and keep a note of 2 aspects: the first is the goal of copying dependencies that commands Maven to copy dependencies to an output folder specified in the settings. The other is that we are going to create an executable and classpath-aware jar file, and for this, we need to configure the manifest and add classpath with all dependencies and also explicitly mention the main class. This process provides flexibility to developers to perform as per requirement on each step, but the dependencies are outside the final Jar.
  • Apache Maven Assembly Plugin: This plugin enables developers to aggregate project output, the dependencies, into a single runnable Jar file. Though this method allows the making of a single Jar file, there is no class relocation support.
  • Apache Maven Shade Plugin: This plugin enables developers to package the artifact in an uber-jar, which not only contains all dependencies into one Jar but support the concept of shading. This plugin allows single Jar file creation and advanced control of packaging and class relocation, but with all these comes more complexity.
  • One Jar Maven Plugin: This provides the developer with a custom class loader that loads classes & resources inside an archive from Jar. This method provides clean delegation along with the support of external Jars and native libraries.
  • Spring Boot Maven Plugin: The final method is to have this plugin which enables packaging of executable Jar and allows running of application “in place”. This takes care of most of the issues other methods have like advanced control, running at every accessible location etc. but in doing so adds in some unnecessary Spring and Spring Boot related classes.

Adding any of the above plugins, we run the command of mvn clean install where the main class is present and then get the Jar file in the target folder. Next, post the creation; Finally, post-run the Jar file in any command prompt (keeping all dependencies in their respective locations) by running java -jar <Name of the JAR file>.

Example of Spring Boot Executable Jar

Given below is the example of Spring Boot Executable Jar:

Code:

Logresource.java:

package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Logresource {
@RequestMapping("/")
public String home() {
return "Hello Readers of EduCBA!";
}
}

DemoJarCreateApplication.java – (Main Class):

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoJarCreateApplication {
public static void main(String[] args) {
SpringApplication.run(DemoJarCreateApplication.class, args);
}
}

Creating and running Jar file:

Syntax:

mvn clean install
cd target
java -jar demoeducba-0.0.1-SNAPSHOT.jar

Output:

Creating and running

Spring Boot Executable Jar 2

Spring Boot Executable Jar 3

Conclusion

To conclude, this article has provided us with numerous methods of how executable jars can be created. In addition, we have gone through one example to make the methodology clearer.

Recommended Articles

This is a guide to Spring Boot Executable Jar. Here we discuss the introduction, how to create and use an executable jar in spring boot? and example. You may also have a look at the following articles to learn more –

  1. Maven Repository Spring
  2. Spring Boot DevTools
  3. Spring AOP
  4. Spring Cloud Components
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more