Cannot find resource for java.util package

I have tow properties files:

config-app.properties

config-dev.properties

and I try to read from a fist witch, I also read from the second:

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 this message:

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

Edit: code formatting.

+4
source share
1 answer

This should work, assuming it config-app.propertiesexists in the class path (at the root). You should not receive the error "Could not find resource for package java.util.PropertyResourceBundle."

, , ${smtp.host.name.env}, ResourceBundle. , - .

, , , - :

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");

config-app.properties, config-dev.properties whatever.properties . .

config-app.properties:

smtp.host.name =localhost

config-dev.properties:

smtp.host.name =smtp.gmail.com

: , , - :

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

All Articles