2016-07-12 6 views
0

Windowsのプロンプト(コンソール)を使用してインストールして実行するmavenプロジェクトがあります。そして、私はそれを実行するたびに私が取得FileNotFoundExceptionメモ帳を使用した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を使用している場合。

ファイルが内にあります。/src/main/resources/フォルダです。

また、IDEでプロジェクトを実行すると、例外が発生することなく正常に完了します。

コンソールコマンドを使用すると、何が問題になりますか?

答えて

2

あなたのxmlファイルをクラスパスにロードされますするもう一つの方法は、以下のように春のフレームワークを使用することです

File file = new File(loader.getResource("config.properties").getFile()); 

使用

File file = new File(loader.getResourceAsStream("config.properties")); 

のInsteed:

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); 
resolver.getResources("classpath*:your/package/**/config.properties"); 
+0

[OK]を、 ' getResourceAsStream() 'が機能します。しかし、なぜ説明できますか? – nllsdfx

+1

これは確かにクラスローダーの問題です。 Eclipse IDEには、 'getResource'または' getResourceAsStream'を呼び出すときに 'workspace/test/target/classes/config.properties'からファイルを取得する独自のclassLoaderがあります。しかし、コマンドラインから実行すると、クラスローダーは ' /YOUR_JAR.jar!config.properties'から取得しようとします。このパスはjava.io.File **を作成するのに有効ではなく、' FileNotFoundException ' –