Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

icon not loading

Thread view: 
Brandon McCombs - 21 Jan 2006 00:46 GMT
Hello,

I'm trying to get icons to show in my GUI and they are stored in my jar
file.  If I run my application from within Eclipse the icons show up but
if I take the jar and run it on another machine or even run it outside
of the directory where the regular class files are stored I end up with
an exception because it can't load the file that I'm pointing to.

I'm using this code now:
 setIcon(new ImageIcon(getClass().getResource("\\images\\domIcon.gif")));

and initially I was using just :
 setIcon(new ImageIcon("images\\domIcon.gif"));

but neither of those get the file to load. I'm not using packages and
just have a set of class files and in the same directory I have a folder
called images that stores the gif files. I'm using WinXP also if that
matters.  Any pointers? thanks
Thomas Fritsch - 21 Jan 2006 00:57 GMT
> I'm trying to get icons to show in my GUI and they are stored in my jar
> file.  If I run my application from within Eclipse the icons show up but
[quoted text clipped - 12 lines]
> images that stores the gif files. I'm using WinXP also if that matters.
> Any pointers? thanks

Read the API doc of Class#getResource(String) carefully! You have to use"/",
not "\\".

Signature

"TFritsch$t-online:de".replace(':','.').replace('$','@')

Brandon McCombs - 21 Jan 2006 06:46 GMT
>> I'm trying to get icons to show in my GUI and they are stored in my jar
>> file.  If I run my application from within Eclipse the icons show up but
[quoted text clipped - 15 lines]
> Read the API doc of Class#getResource(String) carefully! You have to use"/",
> not "\\".

Reading the documentation made no sense to me.
If the name begins with a '/' ('\u002f'), then the absolute name of the
resource is the portion of the name following the '/'.

How am I supposed to know if it begins with a '/' or not? That's what
I'm trying to figure out for myself and having the documentation not
tell me does not help.

Otherwise, the absolute name is of the following form:

   modified_package_name/name

Where the modified_package_name is the package name of this object with
'/' substituted for '.' ('\u002e').

I'm not using packages either so I don't see how this helps.  Is it
possible to get the icons to load *without* using the getResource()
method? It shouldn't be this difficult or complex.
Thomas Weidenfeller - 23 Jan 2006 07:32 GMT
> Reading the documentation made no sense to me.
> If the name begins with a '/' ('\u002f'), then the absolute name of the
[quoted text clipped - 3 lines]
> I'm trying to figure out for myself and having the documentation not
> tell me does not help.

Since you have packed the jar, you are the only one who knows where in
the jar the icons are located. And therefore you are the only one who
knows which argument to provide to get them.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Brandon McCombs - 23 Jan 2006 22:27 GMT
>> Reading the documentation made no sense to me.
>> If the name begins with a '/' ('\u002f'), then the absolute name of
[quoted text clipped - 7 lines]
> the jar the icons are located. And therefore you are the only one who
> knows which argument to provide to get them.

and i'm already providing the correct path (just as I have already
stated in the original mesg) where they can be found *within* the jar
file and it will not work. I also state in the original mesg the layout
of my jar file and say that I have an images directory where the gif
files are located so that is what I have as the arg. to the 2 different
methods I tried to use and it still doesn't work.  I get a null pointer
exception when using the getResource() method and no errors ( but still
no icons) when just specifying the path as an arg. to the ImageIcon
constructor.

> /Thomas
Thomas A. Russ - 24 Jan 2006 17:09 GMT
> and i'm already providing the correct path (just as I have already
> stated in the original mesg) where they can be found *within* the jar
> file and it will not work.

Obviously not.  Otherwise it would work.  My guess is that the problem
is that you have placed the images in a top-level directory under your
jar and not in a subdirectory of the place in the jar where the class
file is in which you make the getClass() call.

If you look at the getResource documentation, is says that if the name
doesn't start with "/", then "the package name is prepended to the
resource name."   That means it is looking for an "images"directory
starting at the point in your source tree where the ".class" file is
located.

Have you tried it with a leading "/" character?

Have you tried printing out the URL that getResource returns?  What does
it look like?  It should look like the correct path in the jar file to
where the image is located.  Does it?

Signature

Thomas A. Russ,  USC/Information Sciences Institute

Brandon McCombs - 25 Jan 2006 00:23 GMT
>> and i'm already providing the correct path (just as I have already
>> stated in the original mesg) where they can be found *within* the jar
[quoted text clipped - 4 lines]
> jar and not in a subdirectory of the place in the jar where the class
> file is in which you make the getClass() call.

I don't think I can make it any clearer where the images are located
beyond what I've already said (and short of posting a screenshot of the
contents when viewed in winrar).  I have a bunch of class files and at
the SAME level as the class files in the jar file is an images directory
and within that directory is the icon files. It is the same structure as
what is used in the Sun tutorials.

> If you look at the getResource documentation, is says that if the name
> doesn't start with "/", then "the package name is prepended to the
> resource name."   That means it is looking for an "images"directory
> starting at the point in your source tree where the ".class" file is
> located.

Why would the name start with a "/"?  The folder is images, plain and
simple. I don't see how a / could be introduced which is why the doc
didn't make a bit of sense. From what I can tell there is no "/" but the
package name shouldn't be prepended because I'm not using a package,
which is another reason the doc didn't help.

So if it is looking where you say it should be then it should be finding
my images directory because it is at the same level in the jar as the
class file is located.

> Have you tried it with a leading "/" character?

Yes, I've tried with //, /, \, \\, and all those both at the front of
the path and in between the images folder name and the actual icon file
name such as //images//icon.gif. None of those combinations work.

> Have you tried printing out the URL that getResource returns?
yes
  What does
> it look like?
It was null as I posted in another message when someone asked for the
specific exception that was generated. It was a null pointer exception
which makes sense since the getResource method returns null.
  It should look like the correct path in the jar file to
> where the image is located.  Does it?
No, I'm guessing that if it did I wouldn't have a problem.
Thomas A. Russ - 25 Jan 2006 02:35 GMT
> > Have you tried printing out the URL that getResource returns?
> yes
[quoted text clipped - 3 lines]
> specific exception that was generated. It was a null pointer exception
> which makes sense since the getResource method returns null.

OK.  I guess it's time to look further back up the chain of calls.  What
does getClass() return?  What about getClassLoader()?  This is where the
lookup of resources happens.  If that returns null (meaning the
bootstrap loader is being used), then I'm not sure where to go from
there.

If you get a class loader, can if find your class using the findClass
method?

>    It should look like the correct path in the jar file to
> > where the image is located.  Does it?
> No, I'm guessing that if it did I wouldn't have a problem.

Signature

Thomas A. Russ,  USC/Information Sciences Institute

Brandon McCombs - 09 Feb 2006 00:29 GMT
>>> Have you tried printing out the URL that getResource returns?
>> yes
[quoted text clipped - 16 lines]
>>> where the image is located.  Does it?
>> No, I'm guessing that if it did I wouldn't have a problem.

Not sure if this changes any of your post but I had another post near
the same time as the one you responded to above stating I had fixed the
problem but I found out that I hadn't really done so. I fixed the icons
loading from within a jar file but after going back to Eclipse I found
that the icons *stopped* loading in there so I fixed it one place and
broke it in the original place.

I got it to work in the jar file by putting this in the code:
setIcon(new ImageIcon("LDAPMgr/images/domIcon.gif"));

It probably has something to do with the classpath but I just haven't
taken the time yet to go back and experiment.
Brandon McCombs - 25 Jan 2006 00:37 GMT
I fixed it finally by using the following:
setIcon(new ImageIcon("LDAPMgr/images/domIcon.gif"));

I had to get rid of the getResource() as you can tell and prepend the
main class name to the beginning of the path.

At one point I had:
setIcon(new ImageIcon(LDAPMgr.class.getResource("images\\domIcon.gif")));

but even that didn't work despite the same class file (LDAPMgr) being
specified.

thanks
Daniel - 23 Jan 2006 04:11 GMT
Hello,
I don't know if you have read the Swing tutorial on icons. If not I
advice you to start there
http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html

You do not tell us what exception you get, but I guess a
FileNotFoundException which is probably due to a malformed imagename
and path. Usually I find it easier to work with only / to describe
paths. As this works fine in both windows and unix. (and mac I think,
but I have not tried yet). From the looks of it, I don't think you
should start your path with \\ as this would indicate the root of the
current filesystem (or drive in windows). So my advice is to first try
with just removing the \\ to make it look like this:
setIcon(new
ImageIcon(getClass().getResource("images\\domIcon.gif")));

give it a go and post a more detailed description if you can't seem to
get your head around it.

regards
Daniel

>Hello,
>
[quoted text clipped - 14 lines]
>called images that stores the gif files. I'm using WinXP also if that
>matters.  Any pointers? thanks
Brandon McCombs - 23 Jan 2006 22:38 GMT
> Hello,
> I don't know if you have read the Swing tutorial on icons. If not I
> advice you to start there
> http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html

Yes I followed the tutorial after I couldn't get things to work
correctly inside the jar file.  The icons work properly when I don't try
to run the jar file and most of the tutorial covers icons outside of a
jar file.  The method they say to use is the same as what I'm already
using from what I can tell: myDemo.class.getResource("images/myImage.gif");

And using that pathing scheme I still a null pointer exception.

> You do not tell us what exception you get, but I guess a
> FileNotFoundException which is probably due to a malformed imagename

actually its nullpointer exception

> and path. Usually I find it easier to work with only / to describe
> paths. As this works fine in both windows and unix. (and mac I think,
[quoted text clipped - 4 lines]
>  setIcon(new
> ImageIcon(getClass().getResource("images\\domIcon.gif")));

I tried this and still get an error. The command I use to create a jar
is the following:
K:\myJava\workspace\LDAPMgr>C:\java142\bin\jar cmf MANIFEST.MF
LDAPMgr.jar *.class images\

The error is:
K:\myJava\workspace>java -jar LDAPMgr.jar
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(Unknown Source)
        at
ldapTree$MyTreeCellRenderer.getTreeCellRendererComponent(ldapTree.jav
a:153)

Line 153 is setIcon(new
ImageIcon(LDAPMgr.class.getResource("images\\domIcon.gif")));

Is there anything you can see that I'm doing wrong ?  thank you

> give it a go and post a more detailed description if you can't seem to
> get your head around it.
[quoted text clipped - 20 lines]
>> called images that stores the gif files. I'm using WinXP also if that
>> matters.  Any pointers? thanks
Daniel - 24 Jan 2006 06:01 GMT
>And using that pathing scheme I still a null pointer exception.
I must say I still think it is a naming problem. It seems you give one
name and it is interpeted to point somewhere you are not expecting it
to. I don't know if you want to make the code more complex, but if you
don't mind maybe you should give the MediaTracker a go.
Images are loaded asyncronously in java, which may lead to problems. I
doubt that is actually the problem here, but it is worth a shot. I
can't see any problem with your code, or naming.
Maybe a few tests is in order until you've figured out exactly what is
going wrong. I.e do less on each line of code. Maybe try this

URL url=getClass().getResource("images\\domIcon.gif");
if(url!=null){
    ImageIcon icon=new ImageIcon(url);
    if(icon!=null){
        setIcon(new ImageIcon());
    }else{
               // print something usefull
    }
}else{
//print something usefull
}

I can't really see anything you do wrong, but as I said, take it step
by step. You say it works inside your development enviroment. So take
a look at your classpath settings, obvisouly it is set so it finds the
image, so what is different in path settings there from when you run
it as from a jar.  Take it one step at a time. Perhaps start with
absolute pathnames so you see that it is the image file that causes
the problem even though  it seems like it).

I am affraid I can't offer much more insight than this. But I often
find it helps to take it slowly, and with more lines of code so you
know exactly what causes your problems. When everything is working you
tidy it up a bit.

regards
Daniel


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.