Home > database >  How to read Ubuntu environment variables into SpringBoot Program?
How to read Ubuntu environment variables into SpringBoot Program?

Time:01-20

Here's what I do in SpringBoot on Windows to read an environment variable (location of log folder).

  1. In Windows Server, I set a System environment variable for "LOG_HOME" with the value with the directory that SpringBoot should use to write logs.

  2. In SpringBoot's application.properties, I have:

logging.file.name= ${LOG_HOME}/ws.log

Works great!


But in Ubuntu Linux 20.04, the same approach doesn't work for me at all.

When the WAR file tries to deploy on Ubuntu 20.04 using this similar technique:

  1. (in .bashrc): export LOG_HOME = /home/ubuntu/logs
  2. reboot (to reload the environment for sure)

I get this error in the Tomcat log when trying to deploy the WAR file:

java.lang.IllegalArgumentException: Could not resolve placeholder 'LOG_HOME' in value "${LOG_HOME}/ws.log"

So, it seems that Spring doesn't see the environment variable set in Ubuntu.

I wrote a simple Java program just to check the value of the environment variables and they were all created as expected including the LOG_HOME as shown in Linux "printenv".

If possible, I need a technique that will work on Ubuntu without changing the working SpringBoot implementation on Windows Server.

Thanks in advance for suggestions.

CodePudding user response:

Instead of exporting in shell session like

export LOG_HOME = /home/ubuntu/logs

try this as -D VM argument in your starup command eg: java -cp=xxx mainclass -DLOG_HOME=/home/ubuntu/log

CodePudding user response:

The solution for me posted by the extremely helpful satyesht above, was to edit the Catalina.sh file and add the "-D" name-value pair option under CATALINA_OPTS. Thanks to all who posted. :)

CodePudding user response:

For tomcat, add your environment variables to $TOMCAT_HOME/bin/setenv.sh where $TOMCAT_HOME is the directory of your tomcat installation.

  • Related