How to run your Spring Boot JAR file from the command line |Video upload date:  · Duration: PT5M11S  · Language: EN

Quick guide to build run and manage a Spring Boot JAR from the command line including background run and basic runtime options

If you like tiny wars with the command line and want your Spring Boot app to live outside an IDE this guide is for you. You will learn how to build a runnable JAR with maven or gradle run it with the Java launcher keep it running in the background and hand it off to a proper process manager like systemd when you get tired of babysitting logs.

Build the runnable JAR

Use Maven or Gradle to produce a runnable artifact. Pick whichever build tool your team swears by during standup.

mvn clean package
./gradlew bootJar

The artifact will usually land in target or build/libs and will be named something like myapp.jar. Move to that folder before firing up the JVM.

Run the app with java

Start the app with the Java launcher. This is the simple fast way to test locally or run in a dev VM.

cd target
java -jar myapp.jar
java -jar myapp.jar --server.port=8081

You can override Spring Boot properties on the fly by passing them as arguments as shown above. It is perfectly fine to be lazy in development.

Pass JVM options and environment variables

Set JVM flags and Spring profiles on the same line if you like your life compact.

SPRING_PROFILES_ACTIVE=prod JAVA_OPTS="-Xmx512m" java $JAVA_OPTS -jar myapp.jar

Run in the background and capture logs

If you want the app to keep running after you close the terminal use nohup or hand it to a real process manager. Nohup is quick and dirty and very popular with exhausted devs.

nohup java -jar myapp.jar > app.log 2>&1 &

Check the running process with standard Linux tools and stop it when needed.

ps aux | grep myapp.jar
kill PID

Move to systemd when you stop liking surprises

For anything beyond light duty use systemd. It restarts your app it rotates logs better and it prevents 3 a m wake up calls when the JVM trips over a stray comma.

[Unit]
Description=My Spring Boot App
After=network.target

[Service]
User=spring
Environment=SPRING_PROFILES_ACTIVE=prod
ExecStart=/usr/bin/java -jar /opt/myapp/myapp.jar
SuccessExitStatus=143
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

Drop the file in /etc/systemd/system then run systemctl daemon-reload and systemctl enable --now myapp.service to let systemd take over. This gives you sane restarts log handling and integration with your OS.

Quick checklist for deployment on Linux

  • Build with maven or gradle and locate the JAR in target or build/libs
  • Run with java -jar and pass Spring properties as args for quick tweaks
  • Use nohup for quick background runs and capture logs in a file
  • Use systemd for production style process management and auto restart
  • Consider containers or managed services for large scale deployments

Parting wisdom

All of this covers development and light production needs without orchestration tools. If you want fewer angry pager messages and more sleep use a container platform or a managed service. You will thank yourself later and so will whoever is on call the next night.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.