java 读取properties 文件的几种方法与NoClassDefFoundError错误

读取properties参考

http://blog.csdn.net/senton/article/details/4083127

https://www.jetbrains.com/idea/help/properties-files.html

在设置MySQL用户名等时候用到了。用的开发工具是IDEA ,刚开始时,是将properties文件放在到与读取它的类同一个目录中。

读取properties的代码为static代码块,如下

结果出错

意思是在compile time时类是有的,但是在run time时候没有在classpath中,常出现在static代码块中。我们可以验证确实是上面的加载properties文件时出现了错误。

可能的原因是,IDEA读取resources时,是从resources文件中读取,与eclipse不同。(没有验证)

我们创建resources目录,并将properties文件放到resources中。如下图

再次运行,成功读取。

 

posted @ 2016-02-17 23:01  chuiyuan  阅读(295)  评论(0编辑  收藏  举报