How to read environment variables in Scala

Solution 1:

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.

Solution 2:

There is an object:

scala.util.Properties

this has a collection of methods that can be used to get environment info, including

scala.util.Properties.envOrElse("HOME", "/myhome" )