Lesson · Console, Arguments & the Runtime Environment
The program environment: args, exit codes, system properties
10 min readProgram Environment
How a program talks to the world around it: command-line arguments in main, exit codes for scripts, environment variables, system properties and the JVM's clockwork.
String[] args, precisely
main(String[] args) receives everything after the class name, split on whitespace by the shell before Java ever sees it: java Copy a.txt b.txt delivers [a.txt, b.txt]. Those famous dots in main(String... args) make no difference: varargs is the same signature with friendlier declaration syntax.
args is never null and never empty by contract; it is an empty array when nothing was passed, and the length check is the program's own responsibility. Quoting is the shell's business (java Grep "hello world" file passes one argument with a space in it), which is why argument parsing of anything complicated belongs to a library, and hand parsing belongs to small tools.
Exit codes
System.exit(0) ends the program immediately with status 0 (success); any other number is a failure signal the shell, CI system or calling script can branch on. Reaching the end of main is exit 0 implicitly. Any pending finally blocks run before the JVM stops, but no further instructions in the calling frame execute: exit does not return.
The convention that matters for composability: 0 is success, non-zero is failure, and distinct non-zero codes carry distinct meanings in well-behaved tools (1 generic, 2 usage error, and so on). echo $LASTEXITCODE (PowerShell) or echo $? (Unix shells) is how a script sees what your program claimed.
Environment variables and system properties
System.getenv("HOME") reads the process environment (only reads: the environment is fixed at process start). System.getProperty("user.dir") and friends read the JVM's own configuration: os.name, java.version, file.encoding, user.home. The distinction to keep straight in interviews: env vars come from the operating system and the launching shell; system properties come from the JVM and are settable with -Dkey=value on the command line.
The -D flag is the standard way to pass configuration without touching code: java -Dapp.mode=dev App then System.getProperty("app.mode", "prod") with a default. Reading configuration from both (properties first, environment fallback) is the pattern most deployment frameworks standardise on.
What the JVM brings with it
The runtime contributes more than streams and properties: Runtime.getRuntime().availableProcessors() (how much parallelism the machine offers), Runtime.getRuntime().maxMemory() (the heap ceiling from -Xmx), and the shutdown hook: Runtime.getRuntime().addShutdownHook(thread) registers work to run on exit, Ctrl+C included, which is where servers flush logs and close carefully.
The honest mental model: your main method is one entry path into a managed runtime, and the runtime's facts (environment, properties, clock, processors, shutdown) are readable through System and Runtime rather than passed as parameters. That is why main is static and why the environment is ambient: the program's surroundings were fixed before main began.
Exit code 0 is the contract with everything downstream
A program that succeeds but exits non-zero fails every CI pipeline, cron job and script that calls it; one that fails but exits 0 hides the failure. System.exit(1) on the error path, 0 (or falling off main) on success, is load-bearing interface design, not a formality.
Interview angles
What is in main's args array?+
Everything after the class name on the java command line, split by the shell on whitespace, minus any JVM flags (those before the class name). Never null; empty when nothing was passed. The caller quotes; Java just receives.
System.getenv versus System.getProperty?+
getenv reads the OS environment, fixed at process start, inherited from the launching shell. getProperty reads JVM configuration, settable with -D at launch, covering both JVM facts (java.version) and application values (app.mode).
When do shutdown hooks run?+
On normal exit, System.exit, and most signals (Ctrl+C). They are not guaranteed on SIGKILL or crash, and they must be fast: the JVM is waiting on them. Registered via Runtime.getRuntime().addShutdownHook.