(1)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;
}
(2)java jar 打包发布 spring applicationContext.xml 找不到此文件
打包为runnable jar时 选择
copy required libraries into a sub-fold... 源码和jar 包分开打包
(3)cmd中运行 java -jar 文件乱码
1、工程项目采用字符编码 、preference workspace text file encoding 设置都应为utf-8
2、java -Dfile.encoding=utf-8 -jar 指定虚拟机编码
3、修改cmd 编码
chcp 65001 通过 chcp命令改变代码页,UTF-8的代码页为65001
命令行标题栏上点击右键,选择"属性"->"字体",将字体修改为True Type字体"Lucida Console",然后点击确定将属性应用到当前窗口。