| Author |
Just Another Resource problem
|
Olivier Legat
Ranch Hand
Joined: Nov 17, 2007
Posts: 176
|
|
Howdy folks,
So here is a simplified version of one of a class I made:
At first I tried without the dot '.' before the slash '/' but I was getting a NullPointerException, it seems to work fine on Eclipse with the '.'
However, when making a JAR file and running it with java -jar file.jar I get the exact same nasty error code as I do when I run it in Eclipse with the '.'s. Removing the dots and making the JAR file doesn't work either. I'm very curious as to why this doesn't work because I've used the exact same method before without any problems (without the dots however).
Error code:
Help on this would greatly be appreciated
|
Olly
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Olivier Legat wrote:At first I tried without the dot '.' before the slash '/' but I was getting a NullPointerException, it seems to work fine on Eclipse with the '.'
With a leading "/" Java will look for the files at the root of the current folder / JAR file. The "./" means relative to the class you request the resources with.
However, when making a JAR file and running it with java -jar file.jar I get the exact same nasty error code as I do when I run it in Eclipse with the '.'s. Removing the dots and making the JAR file doesn't work either. I'm very curious as to why this doesn't work because I've used the exact same method before without any problems (without the dots however).
getResource returning null means the resource can't be found. Check if they are in the same folder as your .class files.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Jonas Isberg
Ranch Hand
Joined: Mar 18, 2003
Posts: 118
|
|
(Edit: This is similar to what Rob Prime just said, I kind of missed his post though.)
First I will have to AdmitWhenYouAreGuessing.
If you check the full stack trace there might be a FileNotFoundException
hidden. Make sure that you know which directory the . (dot) resolves to.
The stack trace might show you where the program is searching for the
file.
You might want to post the full stack trace.
(Points for simplifying the class, you are half way to IsolateTheProblem. :-))
|
 |
Olivier Legat
Ranch Hand
Joined: Nov 17, 2007
Posts: 176
|
|
Rob Prime wrote:With a leading "/" Java will look for the files at the root of the current folder / JAR file. The "./" means relative to the class you request the resources with.
Indeed, you're right The resource files are in the same folders as the classes that make use of them, hence why I intended to use "./", but in fact it works fine without adding anything at all before the filename. Such as so:
Apparently the method getResource automatically add "package_path"+"/" before the name if there isn't a '/' already. I can't I didn't spot that first
Thank-you all for your time and help.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
You're welcome.
|
 |
 |
|
|
subject: Just Another Resource problem
|
|
|