How to Read Environment Variables in Scala?

Scala stands for scalable language. It is an object-oriented language that provides support for functional programming approach as well. Everything in scala is an object e.g. – values like 1,2 can invoke functions like toString(). Scala is a statically typed language although unlike other statically typed languages like C, C++, or Java, it doesn’t require type information while writing the code. The type verification is done at the compile time. Static typing allows to building of safe systems by default. Smart built-in checks and actionable error messages, combined with thread-safe data structures and collections, prevent many tricky bugs before the program first runs. In this article, we will see how we can read environment variables in Scala.

Table of Content

  • What are Environment Variables?
  • Reading Environment Variables in Scala
    • Using Scala’s sys Object
    • Using Scala’s Properties Object
    • Using Java’s System Object
  • Conclusion

What are Environment Variables?

Environment variables are a special type of variable that is stored outside the program and can affect how the program runs depending on the computer. For example, the HOME environment variable represents the home directory.

Reading Environment Variables in Scala

There are several different ways in which we can read environment variables in Scala. We are going to see three different ways to achieve the same.

Using Scala’s sys Object

We can use the sys object provided by Scala to interact with environment variables. The env method provided by sys returns an immutable map containing all the environment variables along with their values.

sys.env

Output:

res0: scala.collection.immutable.Map[String, String] = Map(PATH -> /usr/local/bin:/usr/sbin, HOME -> /usr/home)

sys.env

As the env method returns an immutable map object, we can simply reference it to get the value of a specific environment variable.

sys.env(“PWD”)

Output:

res0: String = /path/to/present/directory

sys.env(“PWD”)

We can also make use of the get method of map object to get the value of a specific environment variable.

sys.env.get(“PWD”)

Output:

res0: Option[String] = Some(/path/to/present/directory)

sys.env.get(“PWD”)

The getOrElse method provided by map can also be used to get the value and return some default value if the environment variable doesn’t exist.

sys.env.getOrElse(“FOO”, “undefined”)

Output:

res0: String = undefined

sys.env.getOrElse(“FOO”, “undefined”)

Using Scala’s Properties Object

Scala provides the developers with Properties object which, among other things, contains methods to read environment variables.
The envOrElse method provided can be used to read the value of an environment variable if it exists or return some pre-defined value if it doesn’t exists.

scala.util.Properties.envOrElse(“PWD”, “/some/value”)

Output:

res0: java.lang.String = /path/to/present/directory

scala.util.Properties.envOrElse(“PWD”, “/some/value”)

Similar to the envOrElse method, the Properties object provides envOrNone method which returns None if the environment variable doesn’t exist.

scala.util.Properties.envOrNone(“FOO”)

Output:

res0: Option[java.lang.String] = None

scala.util.Properties.envOrNone(“FOO”)

Using Java’s System Object

As Scala is interoperable with Java, we can also make use of capabilities offered by Java. The System object provided by Java, provides methods for developers to interact with environment variables. Akin to sys.env method, System contains getenv method which returns a Map object containing all the environment variables along with its values.

java.lang.System.getenv()

Output:

res0: java.util.Map[java.lang.String, java.lang.String] ={PATH=/usr/local/bin:/usr/sbin, HOME=/usr/home}

java.lang.System.getenv()

As the getenv method returns a map object, we can simply reference it to read the value of an environment variable.

java.lang.System.getenv(“PWD”)

Output:

res0: java.lang.String = /path/to/present/directory

java.lang.System.getenv(“PWD”)

If the environment variable doesn’t exist referencing it will return null

java.lang.System.getenv(“FOO”)

Output:

res0: java.lang.String = null

java.lang.System.getenv(“FOO”)

Conclusion

In this article, we saw how we can use Scala to read environment variables. We had a chance to go through several methods to achieve so. We first saw the capabilities provided by Scala, sys object and Properties object, and later saw capabilities provided by Java, System object, which we can make use in Scala owing to its interoperability with Java.