Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I can't load a file with getResource

 
Nick Petas
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the full code of the class :

 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nick Petas
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was easy.I typed big1.gif instead of bug1.gif !
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic