Best way to set environmental variables in WebLogic startup

David Hergert picture David Hergert · May 13, 2014 · Viewed 40.6k times · Source

In Oracle WebLogic, what is the best way to set an environmental variable so that it can be accessed by your code? We have third-party apps running WebLogic that look for an environment variable.

Note: We start our managed servers using Node Manager.

I would prefer to be able to set it somewhere in the domain configuration, like in the Server Start tab in the Admin Console, but there seems no good place to do that.

The only way I can see to do it would be

  1. Edit the bin/setDomainEnv.sh to export the environmental variable
  2. Modify nodemanager.properties to have StartScriptEnabled=true

What this does is, forces NodeManager to use the <ms_home>/bin/startManagedWebLogic.sh, which sources setDomainEnv.sh and they will be picked up when NodeManager starts. But you also have to do this on every machine.

Wondering if there is a cleaner way of doing this than mucking with Oracle's startup scripts.

Answer

Jason Alexander picture Jason Alexander · Oct 28, 2014

If you know for certain that none of the common frameworks are in use, like the Spring Framework, and you have code that strictly looks for environment variables, then you must set the environment variables outside of any of the usual configuration files, before the Java process that will be expecting it, is started. Once the Java process is started, environment variables are read-only and final for that process.

Note: If you need Environment Variables for the entire system, use /etc/profile, /etc/bash_profile, /etc/environment, etc. Keep in mind, that setting the variables in these global locations requires that you restart the Node Manager from a fresh login. You do not need to reboot, but profile/environment files are usually only sourced on login.

For apps within just one domain or node, environment variables should be in the startup scripts for the server(s). Editing setDomainEnv.[sh|cmd] or start(Managed)Weblogic.[sh|cmd], is the best option for setting WebLogic environment variables.

However, if the app is using Spring, system properties and environment variables are combined. System properties are highly encouraged and easier to maintain and control.

Ref: What is best practice for setting java system properties, -D or System.setProperty()?

Weblogic Domain environment variables

One of the places to set both system properties or environment variables, is to edit the domain environment script used to start all nodes or servers that share the same WebLogic server installation and domain. Inside < weblogic_domain >/bin/setDomainEnv.sh, (setDomainEnv.cmd on windows), for environment variables, just add them near the top and add comments to document their use.

    export CUSTOM_VAR="test" # UNIX comment to describe environment variable.

For System Properties, you can add command line arguments that will be added to every server by adding a line for EXTRA_JAVA_PROPERTIES, near the top of the file, near the WL_HOME definition, but after the functions and comments.

    EXTRA_JAVA_PROPERTIES="-Denv=TEST"
    export EXTRA_JAVA_PROPERTIES

    WL_HOME="/appl/oracle/middleware/wls/12.1.2.0.0/wlserver"
    export WL_HOME

Weblogic Node-specific environment variables

If you need different Environment Variables for each node that is started up by the same Node Manager, you will have to customize the startup scripts a little more. In that case, edit the < weblogic_domain >/bin/startManagedWeblogic.[sh|cmd] and insert some scripting logic after _export SERVER_NAME_. This way, you can drive your settings based on SERVER_NAME, etc.

Tip: Windows Environment Variables are not case-sensitive with System.getenv(..).