In Java, reading environment variables is done with System.getenv()
.
Is there a way to do this in Scala?
Since Scala 2.9 you can use sys.env
for the same effect:
scala> sys.env("HOME")
res0: String = /home/paradigmatic
I think is nice to use the Scala API instead of Java. There are currently several project to compile Scala to other platforms than JVM (.NET, javascript, native, etc.) Reducing the dependencies on Java API, will make your code more portable.
If Lightbend's configuration library is used (by default in Play2 and Akka) then you can use
foo = "default value"
foo = ${?VAR_NAME}
syntax to override foo if an environment variable VAR_NAME exist. More details in https://github.com/typesafehub/config#optional-system-or-env-variable-overrides