Question

I just read the excellent 12 Factor App document and it really registered with me. In Chapter 3 Config, the author stresses that:

The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.

I'm wondering what this implies for Java projects.

Typically, when it comes to my Java apps, I think of "env vars" as things that I can read from System.getProperty("blah"). Is this what the author is talking about? I don't think this is, because the author explicitly states "and unlike...or other config mechanisms such as Java System Properties...".

If not, what would be an example of an OS-agnostic "env var" that would satisfy this document's definition of "env var"? How would I read it from inside Java code?

Either way, some process has to first set each env var on the OS, so that the var is set & available by the time the app runs. What processes/methods could do this type of pre-run setup?

Était-ce utile?

La solution

Use System.getenv() instead of System.getProperty(). System.getenv() returns value of specified environment variable defined in your OS.

Other, and probably preferable way is to pass selected OS environment variable to your JVM using -D command switch and then retrieve them using System.getProperty(). This way is more cross-platform: you may use the same java code even if specific platform does not support specific variable.

Updating the OS variables is absolutely other task. First, I do not think that you really want to do this. If you still want try to ask another question: probably other solution exists.

There is no cross platform API that does this in JDK and I do not know 3rd party library that dies this too. I personally planned to implement one but did not have time for this so far.

Windows stores variables in registry, so you can add variable to registry. But this does not affect current instance of shell, so your own java process will not see variables that it updated itself. You can however create process and run SET myvar=myvalue. If you want to write to registry you can use any of available libraries. Occasionally I implemented such library too. Its advantage is that it does not run any native code and is very compact. Take a look here.

On Unix you can run myvar=myvalue and if you want other processes to see this variable you have to run export myvar. But it still does not make this variable persisted. If you want this variable to survive OS restart you have to add it to one of the initial scripts (e.g. .bashrc). But this is completely depends on your system, configuration, permissions, shell etc.

Autres conseils

Yes, the author is talking about environment variables. You can read environment variables from java code using the following snippet:

System.getenv(env);

See: http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getenv-java.lang.String-

If you want OS agnostic way, you can use JVM arguments and read using

System.getProperties();

For example, if you want a variable called var1 to be passed, use java -Dvar1=true -jar myJar.jar

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top