← Back to blog

September 25, 2026 · 10 min read

Packaging and Running Your App: From .jar to Your First Deployment

Java & Spring Boot for Beginners · Part 14 of 14

  • #Java
  • #Spring Boot
  • #Beginners
  • #docker
  • #deployment

For thirteen parts, the task API has lived on your laptop. It validates input, stores tasks in a database, has tests, and knows who's calling it. Nobody else can use any of that. When you close the terminal, it's gone.

This last part changes that. We'll build the app into one file, run it without an IDE, take the secrets out of the code, put it in a Docker image, and deploy it somewhere with a public URL. Then I'll be honest about what separates this first deployment from what a company would call production.

One command, one jar

From the project folder:

./mvnw clean package

Maven compiles the code, runs the tests from Part 12, and writes a file to target/:

target/task-tracker-0.0.1-SNAPSHOT.jar

Run it:

java -jar target/task-tracker-0.0.1-SNAPSHOT.jar

That's the whole app. No IDE, no Tomcat to install, no mvnw spring-boot:run. Any machine with a Java runtime can start it with that one line.

This works because of the spring-boot-maven-plugin that Initializr put in your pom.xml back in Part 8. A normal jar only holds your own compiled classes. The plugin repackages it into a fat jar (sometimes called an uber jar): your classes, every dependency jar, and a small launcher that knows how to load jars from inside a jar. Java can't do that on its own.

Anatomy of a Spring Boot fat jar. The outer box is task-tracker.jar. Inside, META-INF/MANIFEST.MF says Main-Class is Spring Boot's JarLauncher and Start-Class is TaskTrackerApplication. BOOT-INF/classes holds your compiled code and application.yml. BOOT-INF/lib holds around a hundred dependency jars, like spring-web, tomcat-embed-core, hibernate-core, and postgresql. The org/springframework/boot/loader folder holds the launcher. Running java -jar starts the launcher, which builds a classpath from BOOT-INF and then calls your main method.

If you're curious, list what's inside with jar tf target/*.jar | head -40. You'll see BOOT-INF/lib/ full of jars you never downloaded yourself. That's Part 6's dependency tree, packed into one file.

Configuration belongs outside the jar

Right now the jar has a problem. It carries its own configuration: the H2 database from Part 10 and, worse, the development JWT secret from Part 13. Every copy of the jar would share that secret, and anyone with the jar could read it.

The rule that fixes this is simple: the same jar runs everywhere, and only the environment changes. Your laptop, a test server and production all run identical bytes. What differs is configuration, and that comes from outside.

Spring Boot makes this easy because it reads configuration from several places and lets the more specific ones win.

Four layers stacked from bottom to top. Bottom: application.yml inside the jar, with safe defaults like server.port 8080. Next: application-prod.yml, only loaded when the prod profile is active, with production-only settings like the H2 console turned off. Next: environment variables such as DATABASE_URL and JWT_SECRET, set by the host and never committed to Git. Top: command-line arguments like --server.port=9090. An arrow on the side says higher layers override lower ones.

In practice you only need the middle two. Add the Postgres driver next to H2 in pom.xml:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

Then create src/main/resources/application-prod.yml:

spring:
  datasource:
    url: ${DATABASE_URL}
    username: ${DATABASE_USERNAME}
    password: ${DATABASE_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: update
  h2:
    console:
      enabled: false

server:
  port: ${PORT:8080}

jwt:
  secret: ${JWT_SECRET}

A few things to notice:

  • ${DATABASE_URL} has no default. If the variable is missing, the app refuses to start. That's what you want. A production app that silently falls back to an in-memory database is much worse than one that crashes on boot.
  • ${PORT:8080} does have a default, because most hosts tell your app which port to listen on through a PORT variable.
  • The dev secret with a default stays in the main application.yml for your laptop. In prod, JWT_SECRET must come from the environment, or the app won't start.
  • ddl-auto: update lets Hibernate create the tables on first run. It's fine for a learning project. Real apps set it to validate and let a migration tool like Flyway own the schema, which we touched on in Part 10.

The full precedence order, typed @ConfigurationProperties classes and startup validation are covered in Spring Boot Configuration: Profiles, Properties, and Secrets, the first part of the Spring Boot in Production series.

Try it locally before involving anyone else's servers:

SPRING_PROFILES_ACTIVE=prod \
DATABASE_URL=jdbc:postgresql://localhost:5432/tasks \
DATABASE_USERNAME=tasks DATABASE_PASSWORD=tasks \
JWT_SECRET=$(openssl rand -base64 48) \
java -jar target/task-tracker-0.0.1-SNAPSHOT.jar

You'll need a local Postgres for that (the next section gives you one in a single command). If the app starts and curl localhost:8080/actuator/health answers {"status":"UP"}, your configuration is sound.

A small Docker image

The jar needs a Java runtime on the machine. A Docker image bundles the runtime and the jar, so the host only needs to know how to run a container. Almost every hosting platform does.

Create a Dockerfile at the project root:

# Stage 1: build the jar
FROM maven:3.9-amazoncorretto-21 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn -q dependency:go-offline
COPY src src
RUN mvn -q package -DskipTests

# Stage 2: run it
FROM amazoncorretto:21-alpine
WORKDIR /app
RUN addgroup -S app && adduser -S app -G app
COPY --from=build /app/target/*.jar app.jar
USER app
EXPOSE 8080
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "-jar", "app.jar"]

It's short, but each line is there for a reason:

  • Two stages. The first image has Maven and a full JDK to build the jar. The second only has a runtime and your jar. Maven, the source code and the local repository stay behind, and the final image is a fraction of the size.
  • pom.xml is copied before src. Docker caches each step. Dependencies change rarely, code changes all the time, so this order means a code change doesn't re-download the internet.
  • -DskipTests because tests belong before the image, not inside it. Run ./mvnw verify first (or let a CI pipeline do it).
  • A non-root user. If someone ever finds a hole in your app, they land in a user that can't do much.
  • MaxRAMPercentage=75 lets the JVM size its heap from the container's memory limit, leaving room for everything else. On a small free-tier instance that matters.

I'm using Amazon Corretto because it's the JDK I've recommended since Part 2. Use 25 instead of 21 if that's what you picked.

Build and run it:

docker build -t task-tracker .

docker network create tasks-net
docker run -d --name tasks-db --network tasks-net \
  -e POSTGRES_USER=tasks -e POSTGRES_PASSWORD=tasks -e POSTGRES_DB=tasks \
  postgres:17

docker run --rm -p 8080:8080 --network tasks-net \
  -e SPRING_PROFILES_ACTIVE=prod \
  -e DATABASE_URL=jdbc:postgresql://tasks-db:5432/tasks \
  -e DATABASE_USERNAME=tasks -e DATABASE_PASSWORD=tasks \
  -e JWT_SECRET=$(openssl rand -base64 48) \
  task-tracker

Inside the Docker network, the app reaches the database by its container name, tasks-db, not localhost. That one catches everybody the first time. Once it's up, repeat the token flow from Part 13 against localhost:8080. If you get a 200 with a bearer token, the image works.

(There's a shortcut if you don't want to write a Dockerfile at all: ./mvnw spring-boot:build-image produces a decent image using Cloud Native Buildpacks. I still think writing the Dockerfile once is worth it, because you'll understand every image you meet afterwards.)

Putting it on the internet

Free tiers change often, so check current terms before you start. At the time of writing, Render is a friendly choice for a first deployment: it builds straight from a Dockerfile in your Git repo, gives you HTTPS out of the box, and has a free Postgres you can use for experiments. Fly.io and Railway work much the same way. The steps below carry over with different button names.

  1. Push the project to GitHub (with the Dockerfile).
  2. Create a Postgres database in the dashboard. Keep its host, database name, user and password handy.
  3. Create a Web Service from your repository. Render detects the Dockerfile and builds it.
  4. Set the environment variables on the service: SPRING_PROFILES_ACTIVE=prod, JWT_SECRET (a long random value), and the three DATABASE_* variables.
  5. Set the health check path to /actuator/health, the endpoint you first hit in Part 8.

There's one trap in step 4. Hosts usually show the database URL as postgres://user:password@host/dbname. That's not the format Java expects. The JDBC URL is jdbc:postgresql://host:5432/dbname, with the user and password in their own variables. Paste the host's version as-is and you'll get a startup error saying the driver doesn't accept the URL, which tells you little about the real cause.

When the deploy turns green, you get a URL like https://task-tracker-xxxx.onrender.com. Get a token, create a task, send the link to a friend. Your code is running on a computer you've never seen. On a free instance, the service goes to sleep when idle, so the first request after a quiet spell can take the better part of a minute. That's the free tier, not your code.

What real production adds

You now have a working deployment, and I don't want to take anything away from that. It's also a long way from what a team running a product would accept, and it's worth knowing where the gap is.

Two columns. Left, what you have now: one instance, deployed by pushing to main, logs in a web dashboard, a database without tested backups, schema managed by Hibernate update, secrets typed into a dashboard. Right, what production adds: two or more instances behind a load balancer, a CI/CD pipeline that runs tests and deploys with a rollback plan, structured logs plus metrics, traces and alerts, automated backups you have actually restored, versioned Flyway migrations, and a secrets manager with rotation.

None of the right-hand column is exotic. Each item exists because at some point a team lost data, shipped a broken build on a Friday, or spent a night guessing why the app was slow. The one I'd do first is the pipeline: tests running on every push and a deploy that only happens when they pass. Most of the other items get easier once that's in place.

The most underrated item is the backup you have restored. A backup nobody has tested is a hope, not a backup.

Looking back at the series

Fourteen parts ago, we started with why Java and Spring Boot are still a good bet. Since then you've:

  • Set up a JDK and an editor, and learned the language basics: types, control flow, classes, collections and generics.
  • Understood how Maven builds a project and what Spring Boot adds on top.
  • Built a REST API, then gave it a real database with Spring Data JPA.
  • Made it strict about input with validation and consistent error responses.
  • Covered it with unit, slice and integration tests.
  • Secured it with Spring Security, roles and JWTs.
  • And today, packaged it, containerized it and shipped it.

That's the full path of a backend feature, from an empty folder to a URL. It's the same path I've followed countless times at work, just with more people and more process around it. If you made it this far and built the project alongside the posts, you know more than you think. The best next step is to change it: add due dates, add users stored in the database, break something on purpose and fix it.

When you're ready for what comes after "it works," the next series on this blog, From Mid-Level to Senior, picks up where this one stops: CI/CD pipelines your team trusts, deployment strategies, observability, system design, and the less technical skills that decide who gets to make the bigger calls. Thanks for building along. See you there.