• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

I can't load a file with getResource

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the full code of the class :

 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was easy.I typed big1.gif instead of bug1.gif !
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic