Spring Boot Gradle how to build executable jar
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
An executable Spring Boot JAR is a packaged application that includes your app classes and the runtime dependencies needed to start it directly with java -jar. With Gradle, the normal way to build that artifact is to apply the Spring Boot plugin and run the bootJar task.
Use the Spring Boot Gradle Plugin
The plugin creates the executable archive and wires the classpath correctly. A minimal build.gradle using the Groovy DSL looks like this:
That is enough for a basic web app. The important plugin is org.springframework.boot, because it adds the packaging behavior that creates the runnable archive.
Build the Executable JAR
From the project root, run:
On Windows:
The built file usually appears in:
You can then run it directly:
If the app starts, the JAR is truly executable in the Spring Boot sense.
Know the Difference Between jar and bootJar
This is the part that trips people up. Gradle's standard jar task creates a plain JAR of your compiled classes. That is not the same thing as a Spring Boot executable archive.
Spring Boot's bootJar task creates a layered archive with your app plus its dependencies arranged so the Spring Boot launcher can start it.
In other words:
- '
jarbuilds a plain Java archive' - '
bootJarbuilds the runnable Spring Boot archive'
For most Boot applications, bootJar is the task you actually want.
Main Class Detection
Spring Boot normally detects your application entry point automatically if you have a standard main method:
If Gradle cannot determine the main class, you can configure it explicitly:
That removes ambiguity in multi-module or unusual project layouts.
After the build, it is also worth inspecting build/libs directly so you know which archive name your CI or deployment script should pick up. That small habit prevents "build succeeded but deploy script used the wrong file" errors.
When You Also Need a Plain JAR
Sometimes you want both archives, for example if another system expects a plain library JAR. In that case you can keep jar enabled and customize names separately.
Most standalone Boot services do not need that, but it is useful to know the distinction rather than assuming all JAR tasks are interchangeable.
Common Pitfalls
- Running
gradle jarand expecting a fully executable Spring Boot archive. - Forgetting the Spring Boot Gradle plugin, which means
bootJaris not available. - Building successfully but not testing the artifact with
java -jar. - Misconfigured main-class detection in nonstandard project layouts.
- Mixing old plugin examples with current Spring Boot versions without checking the plugin syntax and Java baseline.
Summary
- To build a runnable Spring Boot JAR with Gradle, apply the Spring Boot plugin and run
bootJar. - The executable artifact is usually written under
build/libs. - '
bootJaris different from the plainjartask.' - A standard
mainmethod is usually detected automatically, but you can configure it explicitly if needed. - Always verify the result by starting it with
java -jar, not just by trusting that the build succeeded.
Related reading
- Spring Boot /h2-console throws 403 with Spring Security 1.5.2
- Spring Boot Handler dispatch failed; nested exception is java.lang.NoSuchMethodError
- Spring Boot Hibernate and Flyway boot order
- Spring boot hikari - dataSource or dataSourceClassName or jdbcUrl is required issue
- Spring Boot hotswap with IntelliJ IDE
- Spring Boot How do you specify an environment variable that has dashes in the application.properties?
- Spring Boot How to add another WAR files to the embedded tomcat?
- Spring Boot How to disable startup log message Starting Application on mbp with PID 99446

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.