views:

246

answers:

1

Here's my structure:

  • com/mycompany/ValueReader.class
  • com/mycompany/resources/values.xml

I can read the file in my Eclipse project, but when I export it to a .jar it can never find the values.xml.

I tried using ValueReader.class.getResource() and ValueReader.class.getResourceAsStream() but it doesn't work.

What's the problem here? How do I get a File-object to my values.xml?

B.

+3  A: 

You can't get a File object (since it's no longer a file once it's in the .jar), but you should be able to get it as a stream via getResourceAsStream(path);, where path is the complete path to your class.

e.g.

/com/mycompany/resources/values.xml
Brian Agnew
I tried it and it didn't work.Your approach was correct tho.I used getResourceAsStream(path), but instead of path=com/mycompany/resources/values.xml, i used path=resources/values.xml and it's working.Thanks anyway!
B. T.
it hasn't worked because you didn't put the leading slash, which means the root of the classpath. Without it the path is relative.
Bozho
Ah true.Thanks. I'll never forget this anymore!
B. T.