Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala sys.env for undefined environment variables

This expression

sys.env("HOME")

delivers the path to a home folder; in general sys.env("EnvVar") delivers the value associated to $EnvVar. However, for an environment variable $LIB not declared

java.util.NoSuchElementException: key not found: LIB

How to find out whether the variable is declared without having to catch an exception ?

like image 561
elm Avatar asked Jan 09 '15 08:01

elm


2 Answers

sys.env is a Map, so you can call its get method, which returns an Option:

scala> sys.env.get("FOO")
res0: Option[String] = None
like image 191
Ionuț G. Stan Avatar answered Sep 24 '22 19:09

Ionuț G. Stan


You can try this:

  val value = sys.env.get("EnvVar")

  println(value.getOrElse("Your default value"))
like image 35
user5102379 Avatar answered Sep 20 '22 19:09

user5102379