Java 从默认包加载配置文件与从类路径加载配置文件
在jetty上的JAX-RS应用程序中加载配置文件时,我遇到了一个疯狂的错误Java 从默认包加载配置文件与从类路径加载配置文件,java,classpath,default-package,tinylog,Java,Classpath,Default Package,Tinylog,在jetty上的JAX-RS应用程序中加载配置文件时,我遇到了一个疯狂的错误 public class Configuration { public static final Properties config = new Properties(); static { config.clear(); try (InputStream inputStream = Configuration.class.getClassLoader().getRe
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);
}
}
}
如果我把config.properties放在默认包中,它就可以正常工作了。但当我用java-cp config.properties加载它时,config对象中没有任何内容。我已经研究过这些方法,但它不起作用
ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties");
ClassLoader.getSystemClassLoader().getResourceAsStream("/config.properties");
Configuration.class.getClassLoader().getResourceAsStream("config.properties");
Configuration.class.getClassLoader().getResourceAsStream("/config.properties");
奇怪的是,我在项目中使用Tinylog作为记录器,Tinylog以相同的方式加载其配置文件,即Tinylog.properties:
/* I found this from Tinylog source code */
Configurator.class.getClassLoader().getResourceAsStream("tinylog.properties");
我正在通过java-cp tinylog.properties提供tinylog配置。我的代码有什么问题吗?选项获取目录和jar文件的列表。它不接受属性文件。您需要将包含config.properties文件的目录放在类路径中