找不到包 java.util 的资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25853512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:18:35  来源:igfitidea点击:

Can't find resource for bundle java.util

javajakarta-ee

提问by

I have tow properties files :

我有两个属性文件:

config-app.properties

config-app.properties

config-dev.properties

config-dev.properties

and i try to read from the fist witch read also from the second like that :

我试着从拳头女巫那里读出来,也从第二个女巫那里读出来:

the java code:

Java代码:

String smtpHostName = ResourceBundle.getBundle("config-app").getString("smtp.host.name");

config-app.properties

config-app.properties

smtp.host.name  =${smtp.host.name.env}

config-dev.properties

config-dev.properties

smtp.host.name.env  =smtp.gmail.com

but i have that message :

但我有这样的消息:

Can't find resource for bundle java.util.PropertyResourceBundle

Edit: Code formatting.

编辑:代码格式。

回答by Andres Olarte

This should work, assuming config-app.propertiesexists in the class path (in the root). You should not get the error "Can't find resource for bundle java.util.PropertyResourceBundle".

这应该有效,假设config-app.properties存在于类路径中(在根中)。您应该不会收到错误“找不到包 java.util.PropertyResourceBundle 的资源”。

However, what you're trying to do, by substituting ${smtp.host.name.env}, will NOT work with ResourceBundle. You would need another library to do something more complicated like that.

但是,通过替换 ${smtp.host.name.env},您尝试执行的操作不适用于 ResourceBundle。你需要另一个库来做一些更复杂的事情。

UPDATED

更新

It's not clear what you're trying to do, but assuming you want to have profile for developement and another one for production, you could try to do something like this:

目前尚不清楚您要做什么,但假设您想要开发配置文件和生产配置文件,您可以尝试执行以下操作:

Properties defaultProperties = new Properties();
defaultProperties.load(new FileInputStream("config-app.properties"));

Properties properties = new Properties(defaultProperties);
if (isDev()) {
  properties.load(new FileInputStream("config-dev.properties"));
} else {
  properties.load(new FileInputStream("whatever.properties"));
}

properties.getProperty("smtp.host.name");

This will have default settings in config-app.properties, which can be overwritten in config-dev.propertiesor whatever.propertiesas needed. In this case the keep must be the same.

这将在 中具有默认设置config-app.properties,可以在其中config-dev.propertieswhatever.properties根据需要覆盖。在这种情况下,保持必须相同。

config-app.properties:

config-app.properties:

smtp.host.name =localhost

config-dev.properties:

config-dev.properties:

smtp.host.name =smtp.gmail.com

One last note, the previous code is loading the files from the filesystem, if you have them in the classpath, use something like :

最后要注意的是,前面的代码是从文件系统加载文件,如果类路径中有它们,请使用以下内容:

defaultProperties.load(Classname.class.getClassLoader().getResourceAsStream("config-app.properties"));