Build and Run a Jar
Package your whole app — code plus the web server — into one runnable jar file you can run anywhere Java is installed.
What you will learn
- Build an executable jar with Maven
- Run the app with java -jar
- Set the port for deployment
One file to rule them all
To share or deploy your app, you package it into a single jar file (a JAR — Java ARchive — is one zipped file that bundles compiled Java code together). A Spring Boot jar is special: it includes everything — your code, all libraries, and the web server. Anywhere with Java can run it, no extra setup.
The deployment flow, step by step
Going from your editor to a running app on a server is just a few steps. Here is the whole flow in order; we do each one below:
- Build the jar with
./mvnw clean package— Maven compiles your code and bundles it with every library and the server (Step “Build it” below). - Find the finished file in the
targetfolder, e.g.target/demo-0.0.1-SNAPSHOT.jar. - Run it with
java -jar target/demo-0.0.1-SNAPSHOT.jar— the jar starts its own Tomcat server (Step “Run it” below). - Set the port if the host requires a specific one, via
server.portinapplication.properties(Step “Set the port” below). - Deploy: copy that one jar to a server (or into a Docker container) and run the same
java -jarcommand there — your API is now live for everyone.
Build it
Use the Maven wrapper to build. The jar lands in the target folder:
./mvnw clean package
# the result appears in: target/demo-0.0.1-SNAPSHOT.jarNote: Output: [INFO] BUILD SUCCESS [INFO] Building jar: target/demo-0.0.1-SNAPSHOT.jar Maven compiled your code and bundled it with every dependency and the Tomcat server into one runnable file.
Run it
Run the jar with plain Java — no Maven, no IDE needed:
java -jar target/demo-0.0.1-SNAPSHOT.jarNote: Output: Tomcat started on port(s): 8080 (http) Started DemoApplication in 1.6 seconds The app runs exactly as it did in your editor — because the jar carries its own server. This is the file you put on a real server.
Set the port for the host
Cloud hosts often tell your app which port to use through an environment variable (a named setting the operating system or host gives your program when it starts, e.g. PORT=5000, kept outside your code so it can change per machine). Spring reads it if you map it in application.properties:
# application.properties
# use the host PORT variable, or 8080 when running locally
server.port=8080Note: Output: Locally: the app starts on 8080 On a host that supplies its own port: point server.port at that value The app adapts to wherever it runs — local machine or cloud — by changing one setting, not your code.
Tip: A Spring Boot jar is self-contained, which makes it perfect for cloud platforms and Docker containers: copy one file, run java -jar, and your API is live.
Watch out: Build with clean package, not just package, when something looks stale — clean removes old build files first so you do not accidentally run an out-of-date jar.
Q. Why can a Spring Boot jar run with just “java -jar” and nothing else installed?
✍️ Practice
- Build your project into a jar with
./mvnw clean package. - Run it with
java -jarand confirm the API still works at localhost:8080.
🏠 Homework
- Package one of your APIs into a jar, run it, and write the two commands needed to build and run it from scratch.