Unravel โบ Java concepts โบ Language basics
System, Runtime and environment
System gives access to the clock, environment variables, system properties and standard streams.
At a glance
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).
- 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
- 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
- 3Useful properties: java.version, os.name, user.home, line.separator, file.separator, java.io.tmpdir
- Twelve-factor style config uses environment variables for secrets and settings
- Never log all environment variables or system properties: they contain secrets
- Environment variables are copied by the JVM from the operating system when it starts, so changing them later with other tools is not visible
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; }}- line 3 uses getenv, the core of System, Runtime and environment
- line 4 uses getProperty, the core of System, Runtime and environment
- line 7 uses nanoTime, the core of System, Runtime and environment
- 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
getenvnever returns null. - Using
\nin output files whereSystem.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:
- Method calls:
System.currentTimeMillis()System.nanoTime()System.getProperty()System.getenv()System.lineSeparator()System.identityHashCode()System.setProperty()System.getProperties()System.setOut()System.setErr()System.getenv()
Part of a learning path
- The JVM and the environment ยท previous: The main method, arguments and console input ยท next: Processes and shutdown hooks
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.