I'm trying to read an initial text file within a .jar file. I tried to load the file with ClassLoader, but it didn't work. The strange thing is that this way it worked within Eclipse, but it didn't as separate executable jar file.
Now I'll try to recreate the situation:
Class 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;
}
Class Reader:
private File importedFile;
public void read() {
...
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(FileLoader.load(importedFile.
getPath()));
}
Class 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;
}
I'll be grateful to receive your advice. Thank you!