javaspring-bootwarjvm-arguments

How to pass user.timezone as a JVM argument when running a SpringBoot based war directly using Java


I have a Spring Boot based application packaged as a .war and can execute it directly using Java as follows:

java -jar build/libs/sample-project-0.1.0.war --spring.profiles.active=dev

Is there a way to provide a value for user.timezone as a JVM argument?

I've tried the following but none set the user.timezone property:

java -jar build/libs/sample-project-0.1.0.war --spring.profiles.active=dev -Duser.timezone=America/New_York

java -jar build/libs/sample-project-0.1.0.war --spring.profiles.active=dev -Dspring-boot.run.jvmArguments="-Duser.timezone=America/New_York"

Solution

  • Let's take a look at java command help:

    Usage: java [options] <mainclass> [args...]
               (to execute a class)
       or  java [options] -jar <jarfile> [args...]
               (to execute a jar file)
       or  java [options] -m <module>[/<mainclass>] [args...]
           java [options] --module <module>[/<mainclass>] [args...]
               (to execute the main class in a module)
       or  java [options] <sourcefile> [args]
               (to execute a single source-file program)
    
     Arguments following the main class, source file, -jar <jarfile>,
     -m or --module <module>/<mainclass> are passed as the arguments to
     main class.
    

    in your case:

    java -jar build/libs/sample-project-0.1.0.war --spring.profiles.active=dev -Duser.timezone=America/New_York
    

    since -Duser.timezone=America/New_York argument is specified after -jar option it is considered as main class argument and ignored by JVM, the correct command line is:

    java -Duser.timezone=America/New_York -jar build/libs/sample-project-0.1.0.war --spring.profiles.active=dev