Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

I can't load a file with getResource

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

 
Sheriff
Posts: 21922
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • 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: 41
  • 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: 41
  • 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
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • 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
Posts: 21922
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • 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: 41
  • 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: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was easy.I typed big1.gif instead of bug1.gif !
 
Do not threaten THIS beaver! Not even with this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic