11

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.

4
  • 2
    Do you have a single domain or multiple domains? If you're working with a single domain, adding environment to setDomainEnv or having setDomainEnv source another script seems like a good option. There is no easy way to do this since unlike properties, the environment may not be set at runtime. Another option - if the same user is starting the nodemanagers/servers... you could just set that user's environment in their .bashrc so it's always there when weblogic runs. May 14, 2014 at 0:34
  • Multiple domains, but in this case, its specific to one domain. But even still, we typically have 4 machines in a domain, so it needs to be modified on every machine, or on some sort of shared storage. I like the idea of the user's environment as well, but same caveat still applies, its on every server. I think the real solution is we should just get the code/vendor to read System Properties or files off the classpath. :) May 15, 2014 at 2:50
  • Have you ever manged to solve this?
    – cp5
    Aug 8, 2019 at 16:38
  • Hi @Chrispie - unfortunately not. My suggestion, which @jason-alexander pretty much reiterated, seems to be the best approach. If you can manage your machines with good config management tools (puppet, ansible, etc) then you probably can get away with consistently/reliably setting ENV vars on the OS user or modifying the setDomainEnv file with the necessarily variables. Unfortunately it's not coupling the config with the weblogic config itself, which is what I was shooting for, but I could not find a better way. Aug 14, 2019 at 13:55

1 Answer 1

15

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(..).

3
  • Jason - thanks for the thorough write up! I'm familiar with all three options but the problem here is the vendor code looks for environmental variables, as in System.getenv().get("FOO_HOME")... not System Properties. Nov 25, 2014 at 14:21
  • what about path or ld library path Oct 13, 2016 at 21:21
  • Sure, those would work too, but again, I am trying to configure the setting/management of an environmental variable to the platform (weblogic). So that, say for example, I moved a managed server to a (brand new) machine, I would not have to remember to set that variable. It would be encapsulated into the domain config, tied with the managed server config itself somehow. Its sounding more and more like this is not possible unfortunately. I will admit, Env Variables it not the best way to inject config, but I was at the mercy of the vendor's code unfortunately. Aug 14, 2019 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.