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

转载于:https://www.cnblogs.com/majia1949/p/4532802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值