BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Language basics

Java language

System, Runtime and environment

System gives access to the clock, environment variables, system properties and standard streams.

๐Ÿ”Ž Unravel this example

At a glance

Mental model

The reception desk of the JVM: ask it the time, ask what the building rules are (properties), or check the notices on the wall (environment variables).

What happens, in order
  1. 1System.getenv("DB_URL") reads an environment variable (null if missing); System.getProperty("user.dir") reads a JVM property, set with -Dname=value on the command line
  2. 2System.currentTimeMillis() is wall-clock time and can jump when the clock changes; System.nanoTime() is a monotonic stopwatch, only good for measuring durations, never for dates
  3. 3Useful properties: java.version, os.name, user.home, line.separator, file.separator, java.io.tmpdir
Remember
Watch out
Measuring durations with currentTimeMillisAssuming getenv never returns nullUsing \n in output files where System.lineSeparator() was intended
Example (highlighted lines explain it)
public class Settings {  static String dbUrl() {    String v = System.getenv("DB_URL");    return v != null ? v : System.getProperty("db.url", "jdbc:h2:mem:test");  }  static long timeIt(Runnable r) {    long t0 = System.nanoTime();    r.run();    return (System.nanoTime() - t0) / 1_000_000;  }}
  1. line 3 uses getenv, the core of System, Runtime and environment
  2. line 4 uses getProperty, the core of System, Runtime and environment
  3. line 7 uses nanoTime, the core of System, Runtime and environment
  4. line 9 uses nanoTime, the core of System, Runtime and environment
Read the full explanation (about 1 min)

System gives access to the clock, environment variables, system properties and standard streams. It is how a program reads configuration from the machine it runs on.

A mental model

The reception desk of the JVM: ask it the time, ask what the building rules are (properties), or check the notices on the wall (environment variables).

How it works

System.getenv("DB_URL") reads an environment variable (null if missing); System.getProperty("user.dir") reads a JVM property, set with -Dname=value on the command line.

System.currentTimeMillis() is wall-clock time and can jump when the clock changes; System.nanoTime() is a monotonic stopwatch, only good for measuring durations, never for dates.

Useful properties: java.version, os.name, user.home, line.separator, file.separator, java.io.tmpdir.

Going deeper

Twelve-factor style config uses environment variables for secrets and settings. Provide a default and fail early with a clear message when something required is missing.

Never log all environment variables or system properties: they contain secrets. System.getenv() returns an unmodifiable map, and System.setProperty changes global state for every library in the JVM.

For time use java.time.Clock or Instant.now(); currentTimeMillis arithmetic by hand is a source of bugs.

Under the hood

Environment variables are copied by the JVM from the operating system when it starts, so changing them later with other tools is not visible. nanoTime reads the CPU's high-resolution counter and has an arbitrary origin.

Common mistakes

  • Measuring durations with currentTimeMillis.
  • Assuming getenv never returns null.
  • Using \n in output files where System.lineSeparator() was intended.

How Unravel recognises it

When you paste code, Unravel looks for these signs of system, runtime and environment. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is System, Runtime and environment?

System gives access to the clock, environment variables, system properties and standard streams. It is how a program reads configuration from the machine it runs on.

What are the most common mistakes with System, Runtime and environment?

Measuring durations with currentTimeMillis. Assuming getenv never returns null. Using \n in output files where System.lineSeparator() was intended.

How can I see System, Runtime and environment in real code?

Paste your own Java into Unravel and it will point out where system, runtime and environment appears, explain how it works at the detail level you choose, and draw the flow of the code around it.