Updated April 1, 2023
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.
mvn clean install
2. Running the Jar file through the command line.
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:
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 –