1

I want to set a system property when running a spring boot application in IntelliJ, and then get it in my program. I am using IntelliJ Idea CE.

Initially I tried in a normal Java project (not spring or maven). I just set the value in "VM Options" in run configurations (Ex: -DpropertyName=propertyValue). And then I ran the main function, I was able to access it by System.getProperty("propertyName").

Now I want to do the same with my spring boot project. Its packaging is war. As I am using Community Edition, I run the project by running the maven command spring-boot:run. How do I pass a system property here?

I have tried adding the property in "VM Options" in the run configuration.

I have tried below commands -

spring-boot:run -DpropertyName=propertyValue

spring-boot:run --propertyName=propertyValue

spring-boot:run -Drun.jvmArguments="-DpropertyName=propertyValue"

But in all approaches, when I am getting the property value in my method, it gives null.

Any suggestions are welcome.

EDIT Adding code snipped as requested

public static boolean isSomething() {
    return !StringUtils.equals(System.getProperty("propertyName"), Constants.CONSTANT_VALUE);
}
rsp
  • 813
  • 2
  • 14
  • 26
  • Hey, its already discussed. Please use -Dprop=val in VM arguments. This can be accessed using System.getProperty("prop"). If this a program argument, then it can be accessed using @Value("${program-arg}"). Follow these links [JVM args](https://stackoverflow.com/questions/33635176/how-to-pass-jvm-arguments-in-springboot) [program arguments](https://stackoverflow.com/questions/55868420/how-to-get-command-line-arguments-in-spring-boot) – Dinesh Dontha Aug 20 '20 at 13:44
  • 1
    Tried it. It is even showing in console. But System.getProperty still returns null. Thinking of some work around now. – rsp Aug 20 '20 at 13:45
  • post your code for clarity. you might be missing something – Dinesh Dontha Aug 20 '20 at 13:47

0 Answers0