File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem!! MYClass.class.getResource(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem!! MYClass.class.getResource();" Watch "Problem!! MYClass.class.getResource();" New topic
Author

Problem!! MYClass.class.getResource();

Mario A. Villamizar
Ranch Hand

Joined: Mar 14, 2002
Posts: 35
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,


MA Villamizar
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
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.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Mario A. Villamizar
Ranch Hand

Joined: Mar 14, 2002
Posts: 35
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

Joined: Jun 13, 2000
Posts: 3340
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

Joined: Mar 14, 2002
Posts: 35
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

Joined: Mar 14, 2002
Posts: 35
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

Joined: Mar 14, 2002
Posts: 35
Problem solved
Thanks,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem!! MYClass.class.getResource();