0

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;
}

The .jar file:

I'll be grateful to receive your advice. Thank you!

uccie
  • 183
  • 1
  • 2
  • 9
  • I think it is missing a slas in yur path: http://stackoverflow.com/questions/2552793/reading-xml-file-inside-a-jar-package –  Mar 29 '13 at 13:30
  • @matheszabi I don't think so. – uccie Mar 29 '13 at 14:06
  • Is Welcome.xml inside the object directory present in your jar? What is the exact error you're getting? – Puce Mar 29 '13 at 14:11
  • @Puce Yes, it is inside the object directory. Error message: InputStream cannot be null. – uccie Mar 29 '13 at 14:19
  • Can you check what importedFile.getPath() returns? object/Welcome.xml or object\Welcome.xml? – Puce Mar 29 '13 at 16:10

4 Answers4

1

Have a look at Distributed jar file not picking up my .txt file for an example.

You need to specify the path relative to the class you are using the getResourceAsStream on

Community
  • 1
  • 1
skirsch
  • 1,640
  • 12
  • 24
  • I'm specifying the path absolute to the .jar file: /object/Welcome.xml – uccie Mar 29 '13 at 13:49
  • But it's inside the jar file? then you need to "navigate" down to the root of the jar file by "prepending" ../ for each directory/package the Fileloader class is nested – skirsch Mar 29 '13 at 13:54
  • Can you explain that in context of the given situation? – uccie Mar 29 '13 at 14:01
  • 1
    You load the file(s) in class `FileLoader`, right? So if that class would be located in the package `ba.util`, the correct path to load the file would be `"../../object/Welcome.xml"`; if it would be in `ba.util.resources`, the path needs to be `"../../../object/Welcome.xml"`. The thing with your absolute path is, that it refers to the filesystem of the OS. For windows, this usually translates to `C:\object\Welcome.xml` – skirsch Mar 29 '13 at 16:30
  • The answer is correct, but only with the additional information of the last comment. Thank you! – uccie Mar 29 '13 at 20:35
0

Inside jar file, file structure is little bit different from your actuall structure. You can open your jar to see exatcly path to your file. See also http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

evgenyl
  • 7,837
  • 2
  • 27
  • 32
0

The problem could be that Windows is case insensitive, but a in a jar the file name is case sensitive (as on Linux). Further not use File, and as you did use a / not \.

So I think the actual file is in a bit other case.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
-1

open up your jar file to see if you have exported file or not! -if no -> export your project to jar file again and select the file to export -if yes -> check the path of file

Morteza Adi
  • 2,413
  • 2
  • 22
  • 37