Java Classpath及.properties配置文件
2015-05-27 10:52
295 查看
private static final String CONFIG_FILE = "logmonitor.properties"; ClassLoader cl = ClassLoader.getSystemClassLoader(); input = cl.getResourceAsStream(fileName);
需要改成
ClassLoader cl = PerfCoalescingStatisticsUtil.class.getClassLoader(); input = cl.getResourceAsStream(fileName); prop.load(input);
http://stackoverflow.com/questions/1921238/getclass-getclassloader-is-null-why
Java : Why you should not use getClass().getClassLoader .
http://jeecookbook.blogspot.com/2013/07/java-why-you-should-not-use.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29 http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getSystemClassLoader()
继续相关的,如果配置文件是在一个jar包中,而这个jar包被上一级的工程所引用,如何保证在jar包中的代码能够找到这个配置文件
事实上找到的有用的信息如下
http://www.mkyong.com/applet/how-do-java-applet-access-files-inside-applet-jar-file/ http://stackoverflow.com/questions/5054435/reading-file-in-jar-using-relative-path http://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file http://stackoverflow.com/questions/3627426/loading-a-file-relative-to-the-executing-jar-file
在这种情形下,因为jar包的问题,文件不能如一个妥当的目录被找到
/data/web/apache-tomcat-6.0.43/bin/file:/data/web/apache-tomcat-6.0.43/webapps/ROOT/WEB-INF/lib/log-monitor-api-1.0-SNAPSHOT-jar-with-dependencies.jar!/xcs-online.properties (No such file or directory)
这种情形下,有file:和.jar!标识符,还好找吗?
相关文章推荐
- Linux脚本调用Java模板,将Properties文件放到CLASSPATH中的配置方式
- Linux脚本调用Java模板,将Properties文件放到CLASSPATH中的配置方式
- Java读写Properties配置文件(转载)
- java读取.properties配置文件的几种方法
- java 获取 classpath下的配置文件
- 项目配置文件(xml、properties)路径与classpath的关系
- java对.properties配置文件操作
- Java工程Properties配置文件注释中文,会自动转换为其他编码方式问题解决
- java Io 配置文件Properties练习
- Java Properties 类读取和修改配置文件信息
- Spring配置 在xml和java代码中读取properties文件
- java properties配置文件操作
- Java的properties配置文件
- java读取配置文件.properties(跨包)
- [可用] java读取.properties配置文件的几种方法
- Java读取properties配置文件经常用法
- JAVA 读取Properties配置文件
- Java读取properties配置文件
- java读取.properties配置文件的几种方法
- Java 读取properties配置文件