java resource_error ai_generated true

java.util.MissingResourceException: Can't find bundle for base name

ID: java/missing-resource-bundle

Also available as: JSON · Markdown · 中文
90%Fix Rate
83%Confidence
1Evidence
2024-03-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Java 8 active
Java 11 active
Java 17 active
Java 21 active

Root Cause

The ResourceBundle.getBundle() method cannot locate the properties file or class bundle for the specified base name, usually due to a missing file, incorrect classpath, or wrong locale.

generic

中文

ResourceBundle.getBundle() 方法找不到指定基本名称的属性文件或类包,通常是由于缺少文件、类路径不正确或区域设置错误。

Official Documentation

https://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html

Workarounds

  1. 95% success Ensure the properties file exists in the classpath at the correct location. For example, if base name is `com.example.messages`, place `messages.properties` in `src/main/resources/com/example/`.
    Ensure the properties file exists in the classpath at the correct location. For example, if base name is `com.example.messages`, place `messages.properties` in `src/main/resources/com/example/`.
  2. 90% success Use the full qualified base name with package: `ResourceBundle.getBundle("com.example.i18n.messages")` and ensure the file is at `com/example/i18n/messages.properties`.
    Use the full qualified base name with package: `ResourceBundle.getBundle("com.example.i18n.messages")` and ensure the file is at `com/example/i18n/messages.properties`.
  3. 85% success Provide a default locale bundle to avoid fallback issues: add `messages.properties` (without locale suffix) as a fallback.
    Provide a default locale bundle to avoid fallback issues: add `messages.properties` (without locale suffix) as a fallback.

中文步骤

  1. Ensure the properties file exists in the classpath at the correct location. For example, if base name is `com.example.messages`, place `messages.properties` in `src/main/resources/com/example/`.
  2. Use the full qualified base name with package: `ResourceBundle.getBundle("com.example.i18n.messages")` and ensure the file is at `com/example/i18n/messages.properties`.
  3. Provide a default locale bundle to avoid fallback issues: add `messages.properties` (without locale suffix) as a fallback.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    ResourceBundle expects a .properties file or a class; an empty file with wrong extension is ignored.

  2. 70% fail

    ResourceBundle falls back to the default locale, which may also be missing, causing the same error.

  3. 90% fail

    The base name must include the full package path; moving the file without updating the name breaks the lookup.