Class EnvLoader

java.lang.Object
com.tnsai.config.EnvLoader

public class EnvLoader extends Object
Utility class for loading environment variables from .env file. Automatically loads .env file once on first access.

Searches for .env file in:

  1. Current directory
  2. Parent directory (for Maven multi-module projects)
  3. Two levels up (for deeply nested structures)

Once loaded, environment variables are accessible via:

  • Constructor Details

    • EnvLoader

      public EnvLoader()
  • Method Details

    • ensureLoaded

      public static void ensureLoaded()
      Ensure .env file is loaded. Safe to call multiple times - loads only once.
    • get

      public static String get(String key)
      Get environment variable value. Checks System properties first, then environment variables.
      Parameters:
      key - Variable name
      Returns:
      Value or null if not found
    • get

      public static String get(String key, String defaultValue)
      Get environment variable value with default.
      Parameters:
      key - Variable name
      defaultValue - Default value if not found
      Returns:
      Value or default
    • has

      public static boolean has(String key)
      Check if environment variable is set and non-empty.
      Parameters:
      key - Variable name
      Returns:
      true if set and non-empty