英文:
Java: Unable to locate the resource file with locales
问题
以下是您要翻译的代码部分:
package com.my.exercise;
import java.util.Locale;
import java.util.ResourceBundle;
public class Main {
public static void main(String[] args) {
String language = "en";
String country = "US";
Locale l = new Locale(language, country);
ResourceBundle r = ResourceBundle.getBundle("Bundle", l);
String str = r.getString("wish"); // Prints "Happy Birthday"
System.out.println(str);
}
}
如需进行进一步交流或有其他疑问,请随时提问。
英文:
The following code
package com.my.exercise;
import java.util.Locale;
import java.util.ResourceBundle;
public class Main {
public static void main(String[] args) {
String language = "en";
String country = "US";
Locale l = new Locale(language, country);
ResourceBundle r = ResourceBundle.getBundle("Bundle", l);
String str = r.getString("wish"); // Prints "Happy Birthday"
System.out.println(str);
}
}
works for my sample application with the following folder structure when Resource Bundle 'Bundle' is located in src folder:
But the same code fails with error "Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name Bundle, locale en_US" when Resource Bundle 'Bundle' is placed inside src/main folder:
Problem: What do I need to modify in my code in order to make Resource Bundle 'Bundle' inside src/main folder to be discoverable?
答案1
得分: 0
非常感谢您的建议,VGR!事实上,后端有一个Maven项目。尽管在我的本地Idea项目中无法找到资源文件,我还是提交了代码,但它确实在服务器上通过了编译。我只是确保我使用了位于src/main文件夹中的资源包位置。
英文:
Thank you very much for your advise, VGR! Indeed, there was a Maven project in the back-end. I submitted the code despite it was unable to find the resource files in my local Idea project, but it did pass the compilation on the server. I just ensured that I used the Resource Bundle location within src/main folder.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论