Java Classpath及.properties配置文件

            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!标识符,还好找吗?

posted on 2015-05-27 10:52  majia1949  阅读(886)  评论(0编辑  收藏  举报

导航