我正在使用Maven onejar插件(https://code.google.com/p/onejar-maven-plugin/)来创建一个uberjar。
我想访问一个属性文件,它位于我的类路径的根目录中,如下所示:
Properties prop = new Properties();
try {
prop.load(new FileInputStream("Db.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");
} catch (IOException ex) {
LOG.debug(ex.toString());
}
conn = null;我在同一目录中的log4j.properties文件被找到,因为我可以做日志...我的问题是什么?:/但是找不到Db.properties。
发布于 2013-03-23 02:09:53
FileInputStream用于从文件系统上的文件加载资源。jar中的文件不在文件系统中。您需要使用不同的InputStream。
对于这种情况,建议使用ClassLoader#getResourceAsStream(String)方法。它返回在类路径上找到的InputStream资源。类似于:
InputStream is = getClass().getClassLoader().getResourceAsStream("/Db.properties");应该行得通。或者为了方便起见:
InputStream is = getClass().getResourceAsStream("/Db.properties");值得注意的是,log4j.properties之所以能够工作,是因为Log4j可以在根类路径中加载配置文件。
https://stackoverflow.com/questions/15576985
复制相似问题