I'm getting a crazy error while loading a config file in a JAX-RS application on jetty.
public class Configuration {
public static final Properties config = new Properties();
static {
config.clear();
try (InputStream inputStream = Configuration.class.getClassLoader().getResourceAsStream("config.properties")) {
config.load(inputStream);
} catch (Exception ex) {
Logger.error(ex);
}
}
}
If I put config.properties
in default package, it is working fine. But when I load it with java -cp config.properties
there is nothing in config
object. I have examined these ways but it doesn't work.
ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties");
ClassLoader.getSystemClassLoader().getResourceAsStream("/config.properties");
Configuration.class.getClassLoader().getResourceAsStream("config.properties");
Configuration.class.getClassLoader().getResourceAsStream("/config.properties");
The strange thing is that I use Tinylog as Logger in project and TinyLog loads its configuration file which is tinylog.properties
in the same way:
/* I found this from Tinylog source code */
Configurator.class.getClassLoader().getResourceAsStream("tinylog.properties");
I'm feeding tinylog configuration via java -cp tinylog.properties
. Is there anything wrong with my code?
The
-cp
option takes a list of directories and jar files. It doesn't take properties files. You need to put the directory containing theconfig.properties
file in the classpath.