现象:
使用intellij Idea编写java项目,debug调试时,使用redis.properties文件的类A,A的代码可以使用
“InputStream in = RedisUtils.class.getClass().getResourceAsStream("/" + proPath);”
方式正常读取redisConfig.properties配置文件;但当打包为jar包,A同样的代码方式读取失败,返回null。
原因:
debug时取的是绝对路径,而jar包运行时,取得时jar包根目录的相对路径,redisConfig.properties文件在“BOOT-INF/classes”目录下。
所以改为:
BOOT-INF/classes/redisConfig.properties
注意点:
//jar包路径
private static String proPath = "BOOT-INF/classes/redisConfig.properties";
//debug路径
private static String proPath = "redisConfig.properties";