
Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
...
> URL thePic = this.getClass().getResource("pic0.jpg");
> System.out.println("Pic URL is: " + thePic);
>
> If that is 'null', Java cannot find your image.
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
> OTOH, if it retrusn a valid URL, it might be that
> the image has not loaded yet, but from memory,
> when you construct a JLabel with ImageIcon, all
> that is taken care of.
Paul Lutus - 21 Sep 2004 23:43 GMT
> ...
>>
[quoted text clipped - 9 lines]
>
> I also need to use a relative path, since this has to be portable
In that case, put it and your application in a jar file. This makes locating
the graphic must more likely. You are not likely to be able to use relative
addressing if the required elements are not packaged in some way. It's not
impossible, it's just a lot of effort when creating a jar file is simpler
and has other advantages as well.

Signature
Paul Lutus
http://www.arachnoid.com
Mark Murphy - 22 Sep 2004 04:43 GMT
> ...
>
[quoted text clipped - 7 lines]
> I"ve placed the picture in the same folder as my source code and my .class
> files, before I compile
Try printing:
String dir = System.getProperty("user.dir");
For example if you are using netbeans, netbeans will start Java from
it's home directory. If this is the case then you have to edit the class
path in the IDE or move your image to the location identified. Does the
program see the image when you run it from the command line & you have
the image with the source?
I had this problem too and banged my head against the wall for a long time.
Jacob - 22 Sep 2004 06:14 GMT
> 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{}
Kane Bonnette - 22 Sep 2004 18:29 GMT
...
> 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);
This worked! thanks for the help. I admit I need to study up on classpaths
more if I plan to continue programming.
Andrew Thompson - 22 Sep 2004 07:35 GMT
> ...
>>
[quoted text clipped - 5 lines]
> This turns out to be the issue. Now the question is, what do I need to do to
> get Java to find my file?
How are you invoking Java?
java -cp . TheMainClass
should add the current directory to the class-path
and the 'getResource' will work.
( But as Paul mentioned, ultimately it is better to jar
your project, that sugestion is simply to allow testing. )
For slightly more on finding resources, check this..
<http://www.physci.org/codes/javafaq.jsp#path>
HTH

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane