0

I am having troubles loading an XML file within my java game. Here is where the file is located, (from Eclipse):

package explorer in the Eclipse IDE

I have been doing research, and apparently to use the xml file in a JAR file, I need to call

DocumentBuilder.parse(InputStream)

The problem is when I try to get the InputStream using getResourceAsStream("res/xml/items.xml") it always returns null.

How could I make it not return null? I don't want to put my res folder inside of my "src" folder, but is there some setting in Eclipse that I need to set? What would the correct String of the path be? Thanks!

Edit:

My code:

try {
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("res/xml/items.xml");
    dom = db.parse(is);
} catch(Exception e) {
    e.printStackTrace();
}

This still gives me null.

enter image description here

Keiran Paster
  • 600
  • 2
  • 7
  • 21
  • You need to make "res" a source folder, but that's not going to solve all your problems. Paste the code where you're trying calling `getResourceAsStream()`. – parsifal Mar 20 '13 at 21:39
  • @parsifal InputStream is = Item.class.getResourceAsStream("res/xml/items.xml"); – Keiran Paster Mar 20 '13 at 21:53
  • In the future, please edit your original question with additional information. Comments do not format well. – parsifal Mar 20 '13 at 21:54
  • @parsifal I edited the original post with the problem. It still doesn't work! I tried putting a test document into the "src" folder, and then it works, but if I make the "res" folder a source folder, it never works... – Keiran Paster Mar 20 '13 at 22:28
  • Sorry, I give the quick answer without full explanation. When you make a folder into an Eclipse source folder, that folder gets added to the root of the classpath. So you don't specify the folder name in any paths. Since you accepted my answer, I'm going to assume that you figured that out. I'll edit my response anyway, as it's misleading. – parsifal Mar 21 '13 at 13:02

1 Answers1

4

So, assuming that you tell Eclipse to use "res" as a source folder, you still have two problems with your lookup:

Item.class.getResourceAsStream("res/xml/items.xml");

This is a relative path, so getResourceAsStream() will prepend the directory where Item.class lives (see docs).

The second problem is that Eclipse treats "source" folders as the root of your classpath. So you need to change the paths to exclude "res".

One option is to use an absolute path: "/xml/items.xml"

A better option is to call Thread.currentThread().getContextClassLoader().getResourceAsStream(), which will work correctly in an app-server as well as in a self-contained application (and is a good habit to get into). But you still need to omit "res" from your path.

parsifal
  • 1,246
  • 6
  • 8
  • 1
    Now I am using the Thread method, but it still does not work. I set the folder to a source folder by going to properties -> java build path -> source -> add folder. Which path do I use for the Thread method? – Keiran Paster Mar 20 '13 at 21:58
  • 1
    actually, both methods still give me null. I wonder if I did not make it a source folder correctly? – Keiran Paster Mar 20 '13 at 22:03
  • @KeiranPaster If you didn't got an answer, you had to unaccept that. – peterh Aug 22 '14 at 12:54