You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Guava ImmutableMap中getProperty()引发ExceptionInInitializerError问题排查

ExceptionInInitializerError When Initializing ImmutableMap in Static Utility Class

Problem Context

I have this utility class:

public final class EnvironmentUriAndAuth {
    public static final String ENVIRONMENT = getProperty("environment_type");
    private static final ImmutableMap<EnvironmentConstants, String> AUTH_HEADER = ImmutableMap.of(
        DEV, getProperty("devTestAuth"),
        TEST, getProperty("devTestAuth"),
        PRELIVE, getProperty("preliveAuth"));
    // Private constructor omitted
    public static String getAuthHeader() {
        return AUTH_HEADER.get(EnvironmentConstants.valueOf(ENVIRONMENT));
    }
}

When calling getAuthHeader() from another class, a java.lang.ExceptionInInitializerError is thrown exactly at the line where ImmutableMap is initialized. I suspect it's related to the initialization order of getProperty() calls, but I can't figure out the correct sequence. Is this error caused by initialization issues, or something else?


Yes, This Is Definitely a Static Initialization Issue

The ExceptionInInitializerError directly signals a failure during the static initialization phase of your EnvironmentUriAndAuth class. Here's the breakdown of what's happening:

JVM initializes static variables in the exact order they're declared in the class. For your code:

  1. First, ENVIRONMENT is initialized by calling getProperty("environment_type")
  2. Next, AUTH_HEADER is built by calling getProperty() three times to populate the map values

If any of these getProperty calls throws an uncaught runtime exception (like missing configuration, invalid property sources, or null values that break downstream logic), or if the EnvironmentConstants enum itself has initialization problems (e.g., missing the DEV/TEST/PRELIVE constants), the static initialization fails, and the JVM throws ExceptionInInitializerError.

How to Fix & Debug This

Here are actionable steps to resolve the issue:

  • Audit Your getProperty Implementation

    • If getProperty throws an exception when a property is missing (instead of returning a default value), that's likely the culprit. Update it to handle missing keys gracefully:
      private static String getProperty(String key) {
          // Replace with your actual config read logic
          String value = System.getProperty(key);
          // Return a safe default instead of throwing
          return value != null ? value : "DEFAULT_AUTH_TOKEN";
      }
      
  • Validate the EnvironmentConstants Enum

    • Double-check that DEV, TEST, and PRELIVE are valid constants in the EnvironmentConstants enum. Missing or misnamed constants will throw an error when referenced during ImmutableMap initialization.
    • Ensure the enum itself doesn't have static initialization logic that could fail (e.g., a constructor that calls external services).
  • Use Static Blocks for Explicit Initialization Control

    • Replace inline static variable initialization with a static block to gain full control over the sequence, and add validation steps to catch issues early:
      public final class EnvironmentUriAndAuth {
          public static final String ENVIRONMENT;
          private static final ImmutableMap<EnvironmentConstants, String> AUTH_HEADER;
      
          static {
              try {
                  // Initialize ENVIRONMENT first
                  ENVIRONMENT = getProperty("environment_type");
                  // Validate ENVIRONMENT is a valid enum value upfront
                  if (ENVIRONMENT == null) {
                      throw new IllegalArgumentException("environment_type property is missing");
                  }
                  EnvironmentConstants.valueOf(ENVIRONMENT); // Throws if invalid
      
                  // Build the auth header map
                  AUTH_HEADER = ImmutableMap.of(
                      EnvironmentConstants.DEV, getProperty("devTestAuth"),
                      EnvironmentConstants.TEST, getProperty("devTestAuth"),
                      EnvironmentConstants.PRELIVE, getProperty("preliveAuth")
                  );
              } catch (Exception e) {
                  // Print detailed stack trace to pinpoint the failure
                  e.printStackTrace();
                  throw new ExceptionInInitializerError("Failed to initialize EnvironmentUriAndAuth: " + e.getMessage());
              }
          }
      
          // Private constructor to prevent instantiation
          private EnvironmentUriAndAuth() {}
      
          public static String getAuthHeader() {
              return AUTH_HEADER.get(EnvironmentConstants.valueOf(ENVIRONMENT));
          }
      }
      
    • This approach makes the initialization order explicit and catches invalid environment_type values before they cause further issues.
  • Debug the Exact Cause

    • The try-catch block in the static block will print the full stack trace, showing you exactly which getProperty call or enum reference is failing.

内容的提问来源于stack exchange,提问作者Vitali Plagov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:47:41