MoreExtra· 35 min read

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:

  1. Build the jar with ./mvnw clean package — Maven compiles your code and bundles it with every library and the server (Step “Build it” below).
  2. Find the finished file in the target folder, e.g. target/demo-0.0.1-SNAPSHOT.jar.
  3. Run it with java -jar target/demo-0.0.1-SNAPSHOT.jar — the jar starts its own Tomcat server (Step “Run it” below).
  4. Set the port if the host requires a specific one, via server.port in application.properties (Step “Set the port” below).
  5. Deploy: copy that one jar to a server (or into a Docker container) and run the same java -jar command there — your API is now live for everyone.

Build it

Use the Maven wrapper to build. The jar lands in the target folder:

Building an executable jar with Maven
./mvnw clean package

# the result appears in:  target/demo-0.0.1-SNAPSHOT.jar

Note: 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:

Running the packaged app
java -jar target/demo-0.0.1-SNAPSHOT.jar

Note: 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:

Set the port (read an env var like PORT in production)
# application.properties
# use the host PORT variable, or 8080 when running locally
server.port=8080

Note: 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?

Answer: A Spring Boot executable jar is self-contained — it packages your code, every dependency and the embedded web server, so plain Java can run the whole app.

✍️ Practice

  1. Build your project into a jar with ./mvnw clean package.
  2. Run it with java -jar and confirm the API still works at localhost:8080.

🏠 Homework

  1. Package one of your APIs into a jar, run it, and write the two commands needed to build and run it from scratch.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →