aspose file tools
The moose likes Java in General and the fly likes Just Another Resource problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Just Another Resource problem" Watch "Just Another Resource problem" New topic
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:
Exception in thread "main" java.lang.ExceptionInInitializerError
at tetris.controller.GUIController.<init>
at tetris.Main.main
Caused by: java.lang.NullPointerException
at javax.swing.ImageIcon.<init>
at tetris.GameConstants.<clinit>
... 2 more


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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Just Another Resource problem
 
Similar Threads
image is missing when jaring
Creating a JAR file: File finding problem
Problem with getResource(String) method
need help to find the right path to images
ImageIcon NullPointerException