.jar ファイル内の初期テキスト ファイルを読み込もうとしています。ClassLoader でファイルをロードしようとしましたが、うまくいきませんでした。奇妙なことに、Eclipse 内ではこのように機能しましたが、別の実行可能 jar ファイルとしては機能しませんでした。
次に、状況を再現してみます。
クラスA:
/**
* Read initial file.
*
* @return initial file
*/
private SomeObject readInitialObject() {
String path = "object/Welcome.xml";
File f = new File(path);
Reader r = new Reader(f);
SomeObject o = r.read();
return o;
}
クラスリーダー:
private File importedFile;
public void read() {
...
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(FileLoader.load(importedFile.
getPath()));
}
クラス FileLoader
/**
* Load input stream.
*
* @param path file path
* @return input stream
*/
public static InputStream load(final String path) {
InputStream input = FileLoader.class.getResourceAsStream(path);
if (input == null) {
input = FileLoader.class.getResourceAsStream("/" + path);
}
return input;
}
アドバイスをいただければ幸いです。ありがとうございました!