Pass Command-Line Arguments to Spring Boot Application

In this short Spring Boot tutorial, you will learn how to pass command-line arguments to your Spring Boot application.

Passing Command-Line Arguments

To pass command line arguments to your Spring Boot app when running it with Maven use the

In the below code example, I will pass two command-line arguments: firstName and lastName.

mvn spring-boot:run,--lastName=Kargopolov

Reading Command-Line Arguments

You can read command-line arguments just like you do it in any Java application. In your Spring Boot project find a Java class that contains the public static void main(String[] args) method and read command-line arguments there in the following way:

package com.appsdeveloperblog.examples.commandlinearguments;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class CommandLineArgumentsExampleApplication {

 public static void main(String[] args) {
    // Reading Command-Line arguments
      for(String arg:args) {
      }, args);

Update Values

You can use Command-Line arguments to update values in your file when starting your Spring Boot application. For example, in you file, you can set the server port number to be initially 8080 and then use Command-Line argument to override this value to a different port number.

Default Values in File


Overwrite port number using Command-Line argument: 

mvn spring-boot:run

I hope this very short Spring Boot tutorial was of some help to you.

To learn more about Spring Boot have a look at the list of below video courses. One of them, might be what you need and will help you take your skills to a new level.