Java – How to configure Wildfly to read environment variables

java

When porting an application from the Java application server GlassFish to Wildfly I noticed that applications in Wildfly report environment variables as empty, whereas GlassFish reports a value. The application code is the same; the command to retrieve the environment variable is:

System.getenv("MY_ENV_VAR");

I'm on Linux and setting the environment variable in the .bashrc file of the user who launches GlassFish and Wildfly. I've tried adding the variable to the bin/standalone.conf, bin/standalone.sh, and the bin/.jbossclirc files of Wildfly without any success.

Here is a similar unanswered question on the JBoss Forum:

https://developer.jboss.org/thread/229862

Note: an environment variable and system property are not the same thing. A work around would be to rewrite all of my applications to use system properties instead of environment variables, but I'd like to avoid that if possible.

Best Answer

I got it working by setting the environment variables in the systemd config file as described here:

How to set environment variable in systemd service?

It seems the difference between GlassFish and Wildfly was really with the service scripts as GlassFish was using the older /etc/init.d while Wildfly was using the newer /etc/systemd.