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.combut i have that message :
Can't find resource for bundle java.util.PropertyResourceBundleEdit: Code formatting.
1 Answer
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 =localhostconfig-dev.properties:
smtp.host.name =smtp.gmail.comOne 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")); 4