java jar 发布 读取properties文件失败
用 String path = Test.
class.getResource("/").getPath();
获取根目录后+
文件名读取失败,因此采用JarFile类读取
private static Connection getJdbcConnection() throws Exception{
Connection conn = null;
try {
logger.info("开始获取properties文件");
Properties properties = new Properties();
JarFile jarFile = new JarFile("PPFTest.jar");
ZipEntry zipEntry = jarFile.getEntry("jdbc.properties");
logger.info("获取zipEntry:"+zipEntry);
InputStream in = jarFile.getInputStream(zipEntry);
logger.info("读取到的文件" + in);
properties.load(in);
String jdbcUrl = properties.getProperty(MinaConstUtil.JDBC_JDBCURL);
logger.info("读取到的jdbcUrl :"+jdbcUrl);
String driverClass = properties.getProperty(MinaConstUtil.JDBC_DRIVERCLASS);
String username = properties.getProperty(MinaConstUtil.JDBC_USERNAME);
String password = properties.getProperty(MinaConstUtil.JDBC_PASSWORD);
Class.forName(driverClass);
logger.info("结束获取properties文件");
} catch (Exception e) {
logger.error("关闭数据库连接", e );
throw e;
}finally{
logger.info("关闭数据库连接");
}
return conn;
}