使用记事本的FileNotFoundException [英] FileNotFoundException using Notepad
问题描述
我有一个使用Windows Prompt(控制台)安装和运行的Maven项目.每次我运行它时,我都会得到一个FileNotFoundException
:
I have a maven project which I install and run using Windows Prompt (console). And every time I run it I get a FileNotFoundException
:
public Properties getConfigProperties() throws Exception {
ClassLoader loader = getClass().getClassLoader();
File file = new File(loader.getResource("config.properties").getFile());
config.load(new FileReader(file));
return config;
}
或者NullPointerException
(如果我使用classpath:config.properties
).
config.properties
文件位于 /src/main/resources/
文件夹中.
config.properties
file is inside /src/main/resources/
folder.
如果我在IDE中运行项目,它也会成功完成,不会发生任何异常.
Also if I run the project in the IDE it completes successfully without any exceptions occurring.
那么当我使用console命令时会出什么问题?
So what's the problem when I use the console command?
推荐答案
由
File file = new File(loader.getResource("config.properties").getFile());
使用
File file = new File(loader.getResourceAsStream("config.properties"));
将XML文件加载到类路径中的另一种方法是使用Spring框架,如下所示:
Another way to get your xml file loaded in your classpath is by using the Spring Framework as below:
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
resolver.getResources("classpath*:your/package/**/config.properties");
这篇关于使用记事本的FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!