File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes I can't load a file with getResource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I can Watch "I can New topic
Author

I can't load a file with getResource

Nick Petas
Ranch Hand

Joined: Jan 31, 2007
Posts: 38
I have this line in my code but it returns null although the file exists in the same folder as the java class :


Any ideas?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

getResource has nothing to do with the class location, it is relative to the classpath.
You need to turn your class package into a directory path eg java.lang.String becomes /java/lang/ and then add your file /java/lang/bug1.png
Nick Petas
Ranch Hand

Joined: Jan 31, 2007
Posts: 38
Here is the full code of the class :

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

Nick Petas wrote:I have this line in my code but it returns null although the file exists in the same folder as the java class :


Any ideas?

David is partly wrong. If the file is in the same location as the class file (so LabelFrame.class, not LabelFrame.java) then it is on the class path and it should be loaded properly. I think in your case you've put the image in the same folder with the source file, not the class file.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nick Petas
Ranch Hand

Joined: Jan 31, 2007
Posts: 38
Thank you guys for your help.I used System.getProperty("java.class.path") temprorary to see what is the classpath and then I moved the png file from the src directory to the bin directory that I've go from the getProperty command.
Thank you for your help.

(Here is a nice link about classpath : http://en.wikipedia.org/wiki/Classpath_(Java) )
Nick Petas
Ranch Hand

Joined: Jan 31, 2007
Posts: 38
The only remaining warning from eclipse is that :


Description Resource Path Location Type
The serializable class LabelFrame does not declare a static final serialVersionUID field of type long LabelFrame.java /SimpleGui2/src/myPackage line 12 Java Problem


Any ideas guys of what it is?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Rob Prime wrote:David is partly wrong. If the file is in the same location as the class file (so LabelFrame.class, not LabelFrame.java) then it is on the class path and it should be loaded properly. I think in your case you've put the image in the same folder with the source file, not the class file.

True, I discovered this in the API too
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Nick Petas wrote:The only remaining warning from eclipse is that :

Description Resource Path Location Type
The serializable class LabelFrame does not declare a static final serialVersionUID field of type long LabelFrame.java /SimpleGui2/src/myPackage line 12 Java Problem

Any ideas guys of what it is?

Yes, it's a warning that your serializable class isn't declaring a serialVersionUID field.

http://java.sun.com/developer/technicalArticles/Programming/serialization/

You may or may not actually care; you can either suppress the warning, or give it a serialVersionUID.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

Nick Petas wrote:then I moved the png file from the src directory to the bin directory that I've go from the getProperty command.

I just love being right
Nick Petas
Ranch Hand

Joined: Jan 31, 2007
Posts: 38
I have again the same problem, this time before the constructor.
Check this code :


Any ideas guys on this one?(the images are properly placed in the bin directory of the project along with the classes.
Nick Petas
Ranch Hand

Joined: Jan 31, 2007
Posts: 38
That was easy.I typed big1.gif instead of bug1.gif !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: I can't load a file with getResource