173

In Java, reading environment variables is done with System.getenv().

Is there a way to do this in Scala?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
summerbulb
  • 5,709
  • 8
  • 37
  • 83

7 Answers7

259

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.

paradigmatic
  • 40,153
  • 18
  • 88
  • 147
  • 109
    It's probably better practice to use `sys.env.get("VARIABLE")` which will give you an `Option[String]` rather than throw an error if that variable is missing. – Cristian Vrabie Sep 24 '13 at 14:35
  • 5
    @CristianVrabie I would prefer it also, in most cases. But `sys.env` is just a `Map`. You can whatever method is appropriate for you. – paradigmatic Sep 24 '13 at 14:53
  • 8
    `sys.env.getOrElse("VARIABLE", "default value")` was also helpful in my case where the environment variable may not be defined. – Josh Peak May 06 '20 at 03:51
136

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" )
andy
  • 1,695
  • 1
  • 9
  • 7
  • 3
    I would also prefer Properties. It allows to retrieve Optionals, and has names for commonly used properties. – ppopoff Dec 17 '15 at 14:46
25

Same way:

scala> System.getenv("HOME")
res0: java.lang.String = /Users/dhg
dhg
  • 52,383
  • 8
  • 123
  • 144
  • Now that I use Scala on a daily basis, I must move the accepted answer to @paradigmatic's answer. It uses Scala API and if used as suggested in the comments can return an Option. – summerbulb Jun 08 '15 at 14:45
20

Using directly a default with getOrElse over the sys.env Map (val myenv: Map[String, String] = sys.env):

sys.env.getOrElse(envVariable, defaultValue)

You get the content of the envVariable or, if it does not exist, the defaultValue.

victe
  • 516
  • 6
  • 8
5

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

jfuentes
  • 477
  • 5
  • 8
1

To print all environment variables, you can use

System.getenv.forEach((name, value) => println(s"$name: $value"))
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
  • 1
    `error: missing parameter type` on the args if you just copy-paste this. – thundergolfer Apr 16 '19 at 00:09
  • @thundergolfer: I currently don't have Scala installed on my machine. Did you figure out what was missing? – Matthias Braun Apr 20 '19 at 09:00
  • After changing the parameters to `(name: String, value: String)`, I'm getting `error: type mismatch; found : (String, String) => Unit required: java.util.function.BiConsumer[_ >: String, _ >: String]`. – Roland Weber Jun 17 '19 at 11:36
  • 1
    Here's something that works, but requires an import: https://alvinalexander.com/scala/scala-java-system-environment-variables-properties – Roland Weber Jun 17 '19 at 11:42
  • `sys.env.foreach(tuple => println(s"$tuple"))` is not as elegant but seems to get the job done. – gareth stokes Oct 04 '22 at 00:04
0

You can use sys.props.getOrElse("key", "default") for Java systems properties.

  package scala

  object sys {

  /** A bidirectional, mutable Map representing the current system Properties.
   *
   *  @return   a SystemProperties.
   *  @see      [[scala.sys.SystemProperties]]
   */
  def props: SystemProperties = new SystemProperties

  /** An immutable Map representing the current system environment.
   *
   *  @return   a Map containing the system environment variables.
   */
  def env: immutable.Map[String, String] = immutable.Map(System.getenv().asScala.toSeq: _*)
Henry
  • 2,870
  • 1
  • 25
  • 17