Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem!! MYClass.class.getResource();

 
Mario A. Villamizar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the following structure:
all the source code is in the directory sourcecode
I'm using packages, so the class that has the main method is JTEdi.class and is in the package editor And the class that is actually loading the images is ActionCommand.class and is in the packages editor.action

So I put a couple of flags; one in the contructor of the ActionCommand when it checks is the file exists also when it calls the createImageIcon and always produce the following output:
File exists
Couldn't find file: c:/myproject/sourcecode/images/open
Couldn't find file: c:/myproject/sourcecode/images/new
...

So the file existand I'm giving the right path, but is not loading the image, the createImageIcon returns null value.
the ActionCommand.class.getResource(" ") is not in the main class is that an issue? what could be the problem?
I'm doing all this because I want to be able to load image from a JAR file
Thanks for your help,
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to find resources from a jar, place them there!
Class.getResource() looks for the resource in your classpath!!! The name of the resource never starts with a drive spec when loaded from the classpath.
Place these inside a jar that is in the classpath and change the file name you are looking for to /<directory structure in the jar>/<filename> This should be all you need to do.
 
Mario A. Villamizar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carl Trusiak:
If you want to find resources from a jar, place them there!
Class.getResource() looks for the resource in your classpath!!! The name of the resource never starts with a drive spec when loaded from the classpath.
Place these inside a jar that is in the classpath and change the file name you are looking for to /<directory structure in the jar>/<filename> This should be all you need to do.

Thanks for answer me,
well I know how to create a JAR file, what I did was type jar cmf manifest JTEdi.jar editor images and I creted a manisfest.mf file which has Main-Class: editor.JTEdi and that works fine, but when the application run it doesn't load the images, when I run it from my IDE it loads the images.
At the same time I tried to load the image giving the follow String path "/images/imagename.gif" as you suggested , but it didn't work .
thanks,
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you do a jar -tf JTEdi.jar What string does it give for each image file? This is the string to use in your app preceeded by a /
 
Mario A. Villamizar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carl Trusiak:
When you do a jar -tf JTEdi.jar What string does it give for each image file? This is the string to use in your app preceeded by a /

I did jar -tf JTEdi.jar and I got:
images/open.gif
images/new.gif
images/save.gif
images/print.gif
and I typed
but it doesn't load the images
what could be the problem
 
Mario A. Villamizar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mario A. Villamizar:

but it doesn't load the images
what could be the problem

I tryed this:

And I had the following output:
file:/C:/Documents%20and%20Settings/default/My%20Documents/mscproject/projectSouceCode/bin/images/new.gif
file:/C:/Documents%20and%20Settings/default/My%20Documents/mscproject/projectSouceCode/bin/images/open.gif
file:/C:/Documents%20and%20Settings/default/My%20Documents/mscproject/projectSouceCode/bin/images/save.gif
file:/C:/Documents%20and%20Settings/default/My%20Documents/mscproject/projectSouceCode/bin/images/print.gif

It looks to my that it try to load the images, but what it loads is very small grey square shape; when I rollover the mouse it displays the tooltips and when I click on it it does what it has to do, but it does not displays the icons.
Thanks,
 
Mario A. Villamizar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved
Thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic