Spring Boot: pass system properties in command line

If you want to know how to pass system properties in command line, this page is for you.

This example is a part of  Spring Boot with all examples

Full Code with Spring Boot 2.2.2.RELEASE:

17_spring-boot-system-property-command-line.zip

What you need for run this:

  • Maven 3
  • Java 8

What the code will do:

  • Show how use pass system properties from command line while develop and while using in production



Explain:

In TestRestController, it has this code to show system property named “my_system_properties” like this:

    @GetMapping("/api/test")
    public String test() {
        return System.getProperty("my_system_properties");
    }



Try:

run this in console:

mvn clean spring-boot:run -Dspring-boot.run.jvmArguments="-Dmy_system_properties=test1"

NOTE: if you are using spring 1.5.x or below, you have to change from -Dspring-boot.run.jvmArguments to -Drun.jvmArguments

Then try to access this url:

http://localhost:8080/test/api/test

You should see this

If you want to run as jar, it will be a bit different. Let try.

First, run this in console:

mvn clean package

Then go to the target folder and run this command:

java -Dmy_system_properties=test1"  -jar spring-boot-16-1.0-SNAPSHOT.jar 

Then try to access this url:

http://localhost:8080/test/api/test

Then you should see