Package and Run Spring Boot: mvn package + java -jar
Fast recall
Quick revision
Core answer
repackage execution is configured, mvn clean package creates an executable Boot archive in target, and java -jar target/name.jar launches it. The archive contains nested runtime dependencies; an embedded server is present only if the application depends on a server implementation. Without repackage, ordinary Maven packaging creates its normal JAR or WAR.Understanding of the complete Maven build-to-deployment workflow for Spring Boot applications
Not knowing that the JAR filename includes the version from pom.xml or forgetting that mvn clean is important to avoid stale artifacts
Explain how to pass JVM arguments and Spring properties via command line for production configuration without rebuilding the JAR
Interview-ready guide
Interview answer
To package and run a Spring Boot app with Maven, you follow a two-step process.
First, you build the application by running mvn clean package. The clean part is important — it wipes out your target directory to make sure you're starting fresh without any old compiled code hanging around. Then package compiles your source, runs your tests, and creates the executable Boot archive in the target directory.
jar. This JAR has everything bundled in — your code, all dependencies, and the embedded Tomcat server. It's completely self-contained.
jar. Notice you're using the standard java command here, not Maven. That's a key point — once you have the JAR, you can run it on any machine with Java installed, even if Maven isn't there.
This makes deployment really simple.
A common mistake freshers make is forgetting the clean phase and wondering why deleted code is still executing. xml. You can also pass configuration at runtime without rebuilding.
port=9090 changes the port. jar to limit heap size. This flexibility is huge for production — you build once, then deploy the same JAR to dev, staging, and production with different configurations.
jar and redirect output to a log file, or better yet, use a proper process manager like systemd.
Package and Run Spring Boot: mvn package + java -jar
Direct Answer
When the Spring Boot plugin's repackage execution is configured, mvn clean package creates an executable Boot archive in target, and java -jar target/name.jar launches it. The archive contains nested runtime dependencies; an embedded server is present only if the application depends on a server implementation. Without repackage, ordinary Maven packaging creates its normal JAR or WAR.
Code Example
// Step 1: Build the application
// Terminal command: mvn clean package
// Output: target/order-service-1.0.0.jar (example: 35 MB)
// Step 2: Run with default configuration
// Terminal command: java -jar target/order-service-1.0.0.jar
// Step 3: Run with custom port and profile
// Terminal command:
java -jar target/order-service-1.0.0.jar \
--server.port=9090 \
--spring.profiles.active=production
// Step 4: Run with JVM arguments for production
// Terminal command:
java -Xmx512m -Xms256m \
-Dspring.config.location=/etc/myapp/application.properties \
-jar target/order-service-1.0.0.jar
// Step 5: Run in background (Linux/Mac)
// Terminal command:
nohup java -jar target/order-service-1.0.0.jar \
> application.log 2>&1 &
// Check the process
// Terminal command: ps aux | grep order-service
Common Mistakes & Pitfalls
- Forgetting mvn clean: Freshers often run just mvn package repeatedly, which can include stale compiled classes from deleted source files, leading to mysterious bugs where "I deleted that class but it's still running".
- Wrong JAR filename: Not realizing the JAR name is artifactId-version.jar from pom.xml, so they try java -jar target/myapp.jar when it's actually target/myapp-1.0.0.jar, leading to "file not found" errors.
- Running from wrong directory: Executing java -jar order-service-1.0.0.jar from the root directory instead of specifying the full path target/order-service-1.0.0.jar.
- Not handling background processes: Starting the app with java -jar and then closing the terminal, which kills the process — they need nohup or a process manager like systemd for long-running deployments.
- Ignoring the .original file: Being confused by target/myapp-1.0.0.jar.original (the thin JAR) and trying to run it instead of the repackaged executable Boot archive.
Check your understanding
Answer these without looking back at the article.
- How do you run Spring Boot as a Linux service?
- What is the difference between -jar and -classpath?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.