打包后找不到配置文件
将项目的包名从dmc.ihangmei.com 变更为com.ihangmei.heartbeat后,原先能能够获取的配置信息也不能获取了。
D:\Users\zp\git\heartbeatProject\target>
D:\Users\zp\git\heartbeatProject\target>java -jar 1231-jar-with-dependencies.jar
2017-07-14 17:11:34 Heartbeat init.0: Can't find bundle for base name config/device, locale zh_CN
java.util.MissingResourceException: Can't find bundle for base name config/device, locale zh_CN
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:773)
at com.ihangmei.heartbeat.Heartbeat.initDevConfig(Heartbeat.java:316)
at com.ihangmei.heartbeat.Heartbeat.submain(Heartbeat.java:116)
at com.ihangmei.heartbeat.Heartbeat.main(Heartbeat.java:105)
代码如下:
网上查找资料,如下:
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:836)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:805)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:576)
You know Java is looking for a properties file in a specific locale. You may be baffled why java keeps complaining it can't find a properties file that is right there. A few things to keep in mind when debugging this type of errors:
1. These resource properties files are loaded by classloader, similar to java classes. So you need to include them in your runtime classpath.
2. These resources have fully-qualified-resource-name, similar to a fully-qualified-class-name, excerpt you can't import a resource into your java source file. Why? because its name takes the form of a string.
3. ResourceBundle.getBundle("config") tells the classloader to load a resource named "config" with default package (that is, no package). It does NOT mean a resource in the current package that has the referencing class.
4. ResourceBundle.getBundle("com.cheng.scrap.config") tells the classloader to load a resource named "config" with package "com.cheng.scrap." Its fully-qualified-resource-name is "com.cheng.scrap.config"
修改代码,重新指定其详细地址。变为如下,问题解决。
ResourceBundle resourceBundle = ResourceBundle.getBundle("com.ihangmei.heartbeat.config.device");