Reputation:
I have tow properties files :
config-app.properties
config-dev.properties
and i try to read from the fist witch read also from the second like that :
the java code:
String smtpHostName = ResourceBundle.getBundle("config-app").getString("smtp.host.name");
config-app.properties
smtp.host.name =${smtp.host.name.env}
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.
Upvotes: 4
Views: 38907
Reputation: 4380
This should work, assuming config-app.properties
exists in the class path (in the root). You should not get the error "Can't find resource for bundle 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.
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.properties
or whatever.properties
as needed. In this case the keep must be the same.
config-app.properties:
smtp.host.name =localhost
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"));
Upvotes: 2