您的位置:首页 > 编程语言 > Java开发

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.html
http://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!标识符,还好找吗?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: