16

I would like to pass JVM arguments in the main spring boot class where we will be starting the spring boot application.

Could you please share how to set JVM arguments in spring boot application?

I tried the below options and there is no luck

System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "8080");

or you can use this for spring boot

bootRun {
    systemProperties "property1": "value1", "property2": "value2"
}
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
SpringForLiving
  • 599
  • 2
  • 5
  • 12

11 Answers11

35

Add JVM arguments with -DargumentName i.e.

-DargumentName="value1"

Then in your spring application, you can retrieve the value by doing:

@Value("${argumentName}")
private String myVariable

Hope this helps!

Kabir
  • 860
  • 5
  • 11
16

Spring Boot has a pretty sophisticated environment variable and config properties management. You can do something as simple as this to pass foo with value "bar" via command line.

java -jar app.jar --foo="bar"

If you have not already, the link below has everything you need as far as ways of externalization of configuration in a Spring Boot application:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Update:

If you are running a Spring Boot application, you should always run it using the pattern below:

$ java -jar <jar-name> [--key1="value1"]... [--keyN="valueN"]

And not using the command line suggested in some of the other comments here.

Kartik Pandya
  • 2,718
  • 2
  • 14
  • 28
7

JVM arguments you might add in app.conf file:

export JAVA_OPTS='-Dyour_param=1234'

source: SPRING DOCS

Krzysztof Szewczyk
  • 1,752
  • 1
  • 21
  • 25
5

You can set system properties with the -D switch when you start your application

java -DsomeProperty=123 your.package.name.YourMainClassName

Later in your code you can obtain a property value simply by calling

System.getPropetry("someProperty")
Steven
  • 5,654
  • 1
  • 16
  • 19
5

You can run your app like this:

$ java -server -Dmyproperty=blabla -jar myapp.jar

and you can access the value of myproperty in code.

Pang
  • 9,564
  • 146
  • 81
  • 122
4

You can run your app like this:

$ java -jar app.jar --someProperty=123

in application call:

import org.springframework.core.env.Environment;
@Autowired
private Environment env;

String someProperty= env.getProperty("someProperty");
maciej
  • 51
  • 2
3

You have to use the @Value annotation. For example

@Value("#{systemProperties.test}")

You can use it directly in a bean property, I have an example using in a parameter of a method.

@SpringBootApplication
public class ReviewsMicroserviceApplication {

    public static void main(String[] args) {
        ApplicationContext ctx =  SpringApplication.run(ReviewsMicroserviceApplication.class, args);
        System.out.println(ctx.getBean("value"));
    }

    @Bean
    public String value(@Value("#{systemProperties.test}")String value){
        return value;
    }
}

In order to execute the program you need to add the test property to the JVM properties.

 -Dtest="hallo reos"
reos
  • 8,766
  • 6
  • 28
  • 34
2

When I run Spring Boot Apps with the Maven Plugin I have to set properties with -Dspring-boot.run.jvmArguemtns=xxx but this not really sets System-Properties but Spring-Properties (defined in application.xml)

here the command to start a spring boot app with properties:

mvn spring-boot:run -D"spring-boot.run.jvmArguments=-Dserver.port=8399 -Dactivemq.url=http://localhost:61616"
Lusk116
  • 791
  • 1
  • 6
  • 17
0

enter image description here

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class DemoApplication {

    @Autowired
    MyFantasticClass myFantasticClass;
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    
    @Bean
    CommandLineRunner commandLineRunner() {
        return args -> {
            System.out.println(myFantasticClass.getMyFantasticProperty());
        };
    }
}

@Component
class MyFantasticClass{
    @Value("${myFantasticProperty}")
    String myFantasticProperty;
    
    String getMyFantasticProperty() {
        return myFantasticProperty;
    }
}
jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
0

I was reading this answer that explains application.properties and I started using application.properties to set up my environment-related variables

bootstrap.properties:

slack.channel=test

bootstrap.yml:

slack: 
  channel: ${slack.channel}
Khaled AbuShqear
  • 1,230
  • 14
  • 24
0

If you want your local environment to be always in UTF-8, then the easiest way is to define this inside your POM:

<properties>
    <spring-boot.run.jvmArguments>-Dfile.encoding=UTF-8</spring-boot.run.jvmArguments>
</properties>

You can also add multiple arguments inside it like this:

<properties>
    <spring-boot.run.jvmArguments>-Duser.timezone=UTC -Dfile.encoding=UTF-8</spring-boot.run.jvmArguments>
</properties>

Please note that this only works when you run the Spring Boot application in your local environment with following command:

mvn spring-boot:run

If you are using command line java to run your Spring Boot application, then you should either save the parameter as an environment variable or run Java with the argument like the other answers have mentioned.

Jukka Hämäläinen
  • 647
  • 1
  • 6
  • 17