Re: ImageIcon not finding my images!

From: Jacob (jacob_at_yahoo.com)
Date: 09/22/04


Date: Wed, 22 Sep 2004 07:14:42 +0200

Kane Bonnette wrote:

> This turns out to be the issue. Now the question is, what do I need to do to
> get Java to find my file?
> I"ve placed the picture in the same folder as my source code and my .class
> files, before I compile
>
> I also need to use a relative path, since this has to be portable

Icons should be accessed through the classpath as you indicate.
This means they should be located in your _destination_ directory
(along with your .class files) on your development machine, and
be bundled into the .jar if that's how you deploy. Typically you
set Make or Ant or your IDE up so that it _copies_ images (or make
a symbolic link) from your source area to your destination area.

Code to access an image via classpath:

   URL url = getClass().getResource ("/com/company/path/icon.gif");
   ImageIcon icon = new ImageIcon (url);
   JLabel label = new JLabel (icon);

Beware: Don't use backslash in the path. This is *not* a file name
(even if it looks like that). It is a _resource_ and Java chose the
slash as a delimiter.

Also: Don't forget the initial "/". This is a common error. Probably
because "jar -tf" don't report it.

Note: Most often images are accessed from within the
_current_ package (that is at least how you should organize it; don't
have one single /com/company/images/ repository). In this case your
code will be easier to maintain if you pick the path name from the
current package rather then spelling it out explicitly.

Code:

   String fileName = "icon.gif";
   String packageName = getClass().getPackage().getName();
   String resource = "/" + packageName.replace('.','/') + "/" + fileName;
   URL url = ... // continue with the above

And even if you do have images elsewhere, access it via its package
as outlined here because hardcoding paths like "/com/company/..." breaks
down if your code undergo obfuscating, and: it is easier to move code
around without breaking it.

If you do choose to pick resource from a central repository, consider
making a dummy Java class (or interface) which can be used as a package
handle:

   import com.company.images.Images;
   :
   String packageName = Images.class.getPackage().getName();

"Images" should be as simple as this:

   package com.company.images;
   public interface Images{}



Relevant Pages

  • Re: Favorite CV Libraries
    ... is clear that for high performance vision based apps C/C++ is used to ... Each package seems to cover some basic aspect of vision processing. ... Thus the desire for access to source code or some extension ... acquire images or get access to images. ...
    (sci.image.processing)
  • Re: package and jar files
    ... > I am new to Java. ... This class is placed in the package nl.opk, ... can also include source code, ... .jar files are created using the jar tool supplied with the JDK/SDK. ...
    (comp.lang.java.programmer)
  • Re: How to import exsist java package into Eclipse?
    ... I am new to Java and Eclipse. ... After unzipping, those source code located ... The original source code has some package ...
    (comp.lang.java.programmer)
  • Re: Fetures
    ... It's source code -- it's on the source images with the rest of the ... use of the old kernel source package) -- see the release notes. ... install packages, which will in the future allow you to install stuff ...
    (Fedora)
  • Re: How to import exsist java package into Eclipse?
    ... I am new to Java and Eclipse. ... After unzipping, those source code located ... The original source code has some package ...
    (comp.lang.java.programmer)