Explain Codes LogoExplain Codes Logo

How to start up spring-boot application via command line?

java
spring-boot
maven
gradle
Alex KataevbyAlex Kataev·Nov 24, 2024
TLDR

Launch a Spring Boot application directly via:

Maven:

mvn spring-boot:run

Gradle:

./gradlew bootRun

Executing a built JAR:

java -jar your-app.jar

Remember to swap your-app.jar with your actual JAR filename. Maven/Gradle need to be installed. If not, the frameworks’ respective wrappers, ./mvnw or ./gradlew, are here to save the day!

Getting your house in order

Before cranking up a Spring Boot application, your environment needs a little TLC:

Maven and Gradle folk need these to be installed correctly with bin directory comfortably added to the environment variables.

Got Maven? Your pom.xml should have this Spring Boot Maven plugin:

<!-- Isn't this the cutest plugin you ever saw? --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>

And for Gradle projects, toss in the Spring Boot Gradle plugin into build.gradle:

plugins { // No coffee stains on these plugins id 'org.springframework.boot' version '2.x.x' id 'io.spring.dependency-management' version '1.x.x' id 'java' }

Proxies lurking in your project? Add the proxy settings to your Maven settings.xml. For Gradle fans, adjust your Gradle configurations accordingly.

Finally, remember to get cozy in the directory containing your project's root. Because, where else would the pom.xml or build.gradle be?

Launching the application

Target port: Firewall problems are nasty! Be sure your application’s port is not blocked. Default port is 8080, but feel free to tame it in application.properties or application.yml.

Creating a package: Before the java -jar command, box your code into a JAR file using mvn package or ./gradlew assemble.

Troubleshooting tips

Classpath issues: Ensure Application.java is dressed properly and seated inside the right package and the main method is ready for a power play:

public static void main(String[] args) { // A wild Spring Application appeared! SpringApplication.run(Application.class, args); }

Directory tantrums: Double-check the directory and file names. Typos are not fun when they creep into your runtime.

Level up your startup game

Arguments inputs: Pass arguments to your application by attaching them after your command:

java -jar your-app.jar --server.port=9000 --spring.profiles.active=prod

Using profiles: Taste different application moods using profiles in Spring Boot:

mvn spring-boot:run -Dspring-boot.run.profiles=prod

Log explorations: Keep those logs in sight! They share tales of the application's adventures during startup.