bhylu.com

Advertisements

Java Keep Configuration and Diagnostics Deliberate

An application needs configuration and useful diagnostic messages, but source files and logs are poor places to store or expose secrets.

Understand the idea

Use environment variables, system properties or a suitable configuration layer according to the deployment environment. Validate values at startup rather than passing malformed text deeper into the application. A default is appropriate only when it is safe and documented. Logging should describe an operation and relevant non-sensitive context, not dump passwords, tokens or complete private records.

Run it locally

Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.

JDK and terminal setup

javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out Main

A worked example

Java 21+ · Main.java

public class Main {
    public static void main(String[] args) {
        String configured = System.getProperty("workshop.capacity", "12");
        int capacity = Integer.parseInt(configured);
        if (capacity <= 0) throw new IllegalArgumentException("capacity must be positive");
        System.out.println("Capacity configured: " + capacity);
    }
}

Expected output

Capacity configured: 12

The property has a documented local default. A launcher can supply -Dworkshop.capacity=18 before the class name to choose another value. Parse and validate once at the boundary. A database password should use a suitable secret-delivery mechanism and should never be printed just because a connection failed.

Check the official Java documentation for detailed contracts and supported options.