How to get Java/Scala system environment variables and properties

Want to get the system environment variables and/or properties from your Scala or Java application? This quick post shows what environment variables and properties are available.

Here’s a little Scala application that prints all the environment variables and properties. You’ll see that you can convert it to Java very easily:

import scala.collection.JavaConversions._

object EnvironmentVariables extends App {

    val environmentVars = System.getenv()
    for ((k,v) <- environmentVars) println(s"key: $k, value: $v")

    val properties = System.getProperties()
    for ((k,v) <- properties) println(s"key: $k, value: $v")

}

List of Java/Scala environment variables

Here’s a list of all of the environment variables on my current (old) Mac OS X system, given as output from the environmentVars print loop:

/*  Environment Variables */

key: SHELL, value: /bin/bash
key: TMPDIR, value: /var/folders/h5/h59HESVvEmG+3I4Q8lOAxE+++TI/-Tmp-/
key: com.apple.java.jvmMode, value: client
key: APP_ICON_9820, value: ../Resources/Eclipse.icns
key: __CF_USER_TEXT_ENCODING, value: 0x1F5:0:0

key: PATH, value: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/bin:/usr/local/git/bin
key: COMMAND_MODE, value: unix2003
key: DISPLAY, value: /tmp/launch-p3DxEp/org.x:0
key: USER, value: Al
key: com.apple.java.jvmTask, value: JNI.java

key: HOME, value: /Users/Al
key: LOGNAME, value: Al
key: Apple_PubSub_Socket_Render, value: /tmp/launch-HtLZCW/Render
key: JAVA_MAIN_CLASS_9830, value: misc.EnvironmentVariables
key: SSH_AUTH_SOCK, value: /tmp/launch-PIVsaN/Listeners
key: JAVA_STARTED_ON_FIRST_THREAD_9820, value: 1

List of Java/Scala system properties

Here’s a list of all of the environment variables on my current Mac OS X system, given as output from the properties print loop:

/* Properties */

key: java.runtime.name, value: Java(TM) SE Runtime Environment
key: sun.boot.library.path, value: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries
key: java.vm.version, value: 20.12-b01-434
key: awt.nativeDoubleBuffering, value: true
key: gopherProxySet, value: false

key: mrj.build, value: 10M3909
key: java.vm.vendor, value: Apple Inc.
key: java.vendor.url, value: http://www.apple.com/
key: path.separator, value: :
key: java.vm.name, value: Java HotSpot(TM) 64-Bit Server VM

key: file.encoding.pkg, value: sun.io
key: sun.java.launcher, value: SUN_STANDARD
key: user.country, value: US
key: sun.os.patch.level, value: unknown
key: java.vm.specification.name, value: Java Virtual Machine Specification

key: user.dir, value: /Users/Al/Documents/EclipseScalaWorkspace/ScalaTests4
key: java.runtime.version, value: 1.6.0_37-b06-434-10M3909
key: java.awt.graphicsenv, value: apple.awt.CGraphicsEnvironment
key: java.endorsed.dirs, value: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/endorsed
key: os.arch, value: x86_64

key: java.io.tmpdir, value: /var/folders/h5/h59HESVvEmG+3I4Q8lOAxE+++TI/-Tmp-/
key: line.separator, value:
key: java.vm.specification.vendor, value: Sun Microsystems Inc.
key: os.name, value: Mac OS X
key: sun.jnu.encoding, value: MacRoman

key: java.library.path, value: .:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
key: java.specification.name, value: Java Platform API Specification
key: java.class.version, value: 50.0
key: sun.management.compiler, value: HotSpot 64-Bit Tiered Compilers
key: os.version, value: 10.6.8

key: http.nonProxyHosts, value: local|*.local|10.1/16|*.10.1/16
key: user.home, value: /Users/Al
key: user.timezone, value: 
key: java.awt.printerjob, value: apple.awt.CPrinterJob
key: file.encoding, value: MacRoman

key: java.specification.version, value: 1.6
key: java.class.path, value: /Users/Al/Documents/EclipseScalaWorkspace/ScalaTests4/bin
key: user.name, value: Al
key: java.vm.specification.version, value: 1.0
key: sun.java.command, value: misc.EnvironmentVariables

key: java.home, value: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
key: sun.arch.data.model, value: 64
key: user.language, value: en
key: java.specification.vendor, value: Sun Microsystems Inc.
key: awt.toolkit, value: apple.awt.CToolkit

key: java.vm.info, value: mixed mode
key: java.version, value: 1.6.0_37
key: java.ext.dirs, value: /Library/Java/Extensions:/System/Library/Java/Extensions:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext
key: sun.boot.class.path, value: [VERY LONG PATH, OMITTED]
key: java.vendor, value: Apple Inc.

key: file.separator, value: /
key: java.vendor.url.bug, value: http://bugreport.apple.com/
key: sun.io.unicode.encoding, value: UnicodeLittle
key: sun.cpu.endian, value: little
key: mrj.version, value: 1060.1.6.0_37-434

key: socksNonProxyHosts, value: local|*.local|10.1/16|*.10.1/16
key: ftp.nonProxyHosts, value: local|*.local|10.1/16|*.10.1/16
key: sun.cpu.isalist, value:

How to get specific Java environment variables and properties

To get a specific Java or Scala environment variable, write code like this:

/**
 * Get the value of the USERNAME, like 'Al' or 'Alvin'
 */
def username = System.getenv("USER")

To access a system property from a Java or Scala application, write code like this:

/**
 * Get the user's $HOME directory, like '/Users/Al'
 */
def userHomeDirectory = System.getProperty("user.home")

If you needed information on Scala or Java system properties or environment variables, I hope this has been helpful.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.