Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Where should I place resource-files like images ?

Reply
Thread Tools

Where should I place resource-files like images ?

 
 
Arne Styve
Guest
Posts: n/a
 
      02-20-2006
Hi,

I'm building an application where I want to use buttons (JButton) with
images loaded from files (.GIF-files).

At the moment, I create the ImageIcon's to use like this:
disconIcon = new ImageIcon("resources/images/discon.gif");
lightOnIcon = new ImageIcon("resources/images/LightOn.gif");
lightOffIcon = new ImageIcon("resources/images/LightOff.gif");

and then use the setIcon()-method of the button to set the Icon.

This works, but I'm not quite confortable with it.
What is the best strategy for placing image-files so that they will always
be found, both when running the application from within Eclipse, and when
creating a JAR of the application for distribution ? When creating a JAR, I
would like the image-files to be packed within the JAR. How should I refere
to the image-files to ensure they will always be found independent of where
my code/JAR-file is installed on the target ?

Regards
Arne


 
Reply With Quote
 
 
 
 
Cyril
Guest
Posts: n/a
 
      02-20-2006
Hi,

Arne Styve a écrit :
> What is the best strategy for placing image-files so that they will always
> be found, both when running the application from within Eclipse, and when
> creating a JAR of the application for distribution ? When creating a JAR,I
> would like the image-files to be packed within the JAR. How should I refere
> to the image-files to ensure they will always be found independent of where
> my code/JAR-file is installed on the target ?


Use getClass().getClassLoader().getResource(), this way:

disconIcon = new
ImageIcon(getClass().getClassLoader().getResource( "resources/images/discon.gif"));

The images must be in your class path with the correct path (here
resources/images).

Cheers,

Cyril

 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      02-20-2006
Arne Styve wrote:
> What is the best strategy for placing image-files so that they will always
> be found, both when running the application from within Eclipse, and when
> creating a JAR of the application for distribution ?


You locate the image data via the class loader. See Class.getResource()
to get a URL, and use that URL to load the image.

Spend a minute or two to learn about the difference between specifying
an absolute or relative resource location from the API documentation of
getResource().

> When creating a JAR, I
> would like the image-files to be packed within the JAR. How should I refere
> to the image-files to ensure they will always be found independent of where
> my code/JAR-file is installed on the target ?


See above. getResource() uses a class loader. The default class loader
is typically one of type URLClassLoader or similar and it resolves
locations using the class path. Entries in the class path can be
directories or jars. If the jar with the icons is in the class path
(and(!) you provide the correct resource name string) the icons are
found in the jar.

/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      02-20-2006
Cyril wrote:
> disconIcon = new
> ImageIcon(getClass().getClassLoader().getResource( "resources/images/discon.gif"));
>


disconIcon = new
ImageIcon(getClass().getResource("resources/images/discon.gif"));

or in static context or if you want to avoid subclassing changing the
lookup of relative resources:

disconIcon = new
ImageIcon(<the-class-name>.class.getResource("resources/images/discon.gif"));


1) Class has an own getResource() method which does the work. No need to
dereference the ClassLoader first.

2) Class.getResource() can lock up resources relatively to the current
class, ClassLoader.getResource() doesn't.

/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
Arne Styve
Guest
Posts: n/a
 
      02-20-2006
Hi Thomas (and Cyril),

Thanks for your answers. I'll try it out.

Do you have any strategies regarding where you place resources like this ?
Do you put them in a seperate directory, like I've suggested, or do you
place these types of files in the same directory as the classes that uses
them ?

Regards Arne

"Thomas Weidenfeller" <(E-Mail Removed)> wrote in message
news:dtcbvk$6in$(E-Mail Removed)...
> Arne Styve wrote:
>> What is the best strategy for placing image-files so that they will
>> always be found, both when running the application from within Eclipse,
>> and when creating a JAR of the application for distribution ?

>
> You locate the image data via the class loader. See Class.getResource() to
> get a URL, and use that URL to load the image.
>
> Spend a minute or two to learn about the difference between specifying an
> absolute or relative resource location from the API documentation of
> getResource().
>
>> When creating a JAR, I would like the image-files to be packed within the
>> JAR. How should I refere to the image-files to ensure they will always be
>> found independent of where my code/JAR-file is installed on the target ?

>
> See above. getResource() uses a class loader. The default class loader is
> typically one of type URLClassLoader or similar and it resolves locations
> using the class path. Entries in the class path can be directories or
> jars. If the jar with the icons is in the class path (and(!) you provide
> the correct resource name string) the icons are found in the jar.
>
> /Thomas
> --
> The comp.lang.java.gui FAQ:
> ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
> http://www.uni-giessen.de/faq/archiv....java.gui.faq/



 
Reply With Quote
 
Cyril
Guest
Posts: n/a
 
      02-20-2006
Hi,

Thomas Weidenfeller a écrit :
> 1) Class has an own getResource() method which does the work. No need to
> dereference the ClassLoader first.
>
> 2) Class.getResource() can lock up resources relatively to the current
> class, ClassLoader.getResource() doesn't.


Thanks for that tip. Always eager to learn

Cheers,

Cyril

 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      02-20-2006
Arne Styve wrote:
> Do you have any strategies regarding where you place resources like this ?
> Do you put them in a seperate directory, like I've suggested, or do you
> place these types of files in the same directory as the classes that uses
> them?


It depends

If you want, for example, build a self-contained component (some
JavaBean etc.), it would make sense to have resources like icons
relatively to the class, probably in a subdirectory "resources".

If you have, for example, application-wide resources like application
icons, it would make sense to have them in some absolute location, or
relative to some main class - which would then, from an application's
point of view, also be some kind of absolute location.

/Thomas


BTW: Consider changing your quoting style. Many people on Usenet will
already ignore you for posting the response before the quotation. It
belongs after the quotation.
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
Arne Styve
Guest
Posts: n/a
 
      02-21-2006
"Thomas Weidenfeller" <(E-Mail Removed)> wrote in message
news:dtcbvk$6in$(E-Mail Removed)...
> Arne Styve wrote:
>> When creating a JAR, I would like the image-files to be packed within the
>> JAR. How should I refere to the image-files to ensure they will always be
>> found independent of where my code/JAR-file is installed on the target ?

>
> See above. getResource() uses a class loader. The default class loader is
> typically one of type URLClassLoader or similar and it resolves locations
> using the class path. Entries in the class path can be directories or
> jars. If the jar with the icons is in the class path (and(!) you provide
> the correct resource name string) the icons are found in the jar.
>
> /Thomas
> --
> The comp.lang.java.gui FAQ:
> ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
> http://www.uni-giessen.de/faq/archiv....java.gui.faq/


I've studied the article at
http://java.sun.com/j2se/1.5.0/docs/...resources.html and I also
found one at http://java.sun.com/docs/books/tutor...misc/icon.html.
The latter one was quite detailed and good. BUT I still have the same
problem. I've tried placing the images as subdirectories/packages under the
package where the class is defined that uses the images, and I've tried
placing the images at the root of the packages. It always work when I run my
application from within NetBeans or Eclips, but as soon as I create a JAR
with both the classes and the images, I cannot locate the image-files. I've
followed the details in the ICON-tutorial mentioned above, but still no
success. Is there a way to print the path from where getResource() starts to
search from ?

-Arne

PS! Thanks for your patience..


 
Reply With Quote
 
opalpa@gmail.com opalinski from opalpaweb
Guest
Posts: n/a
 
      02-21-2006
> Is there a way to print the path from where getResource() starts to
> search from ?



say you got:

package com.arne.util;
class Goods {
....
Goods.class.getResource("pic.png");
Goods.class.getResource("pix/a.png");
....
}

getResource looks from where the Goods.class file is. So if that file
is in directory $D then the first getResource gets $D/pic.png and the
second getResource gets $D/pix/a.png .

Opalinski
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.geocities.com/opalpaweb/

 
Reply With Quote
 
Arne Styve
Guest
Posts: n/a
 
      02-21-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>> Is there a way to print the path from where getResource() starts to
>> search from ?

>
>
> say you got:
>
> package com.arne.util;
> class Goods {
> ...
> Goods.class.getResource("pic.png");
> Goods.class.getResource("pix/a.png");
> ...
> }
>
> getResource looks from where the Goods.class file is. So if that file
> is in directory $D then the first getResource gets $D/pic.png and the
> second getResource gets $D/pix/a.png .
>
> Opalinski
> (E-Mail Removed)
> http://www.geocities.com/opalpaweb/
>


Thanks Opalinksi. I've got this to work. But when I create a JAR file with
the class Goods in the package com.arne.util, and with the pix/a.png file
included, I thought I still could use the same getResource("pix/a.png") when
I start the application from the JAR-file like the following:

c:\SomeDir\AppDir>java -cp Goods.jar Goods

The Goods.jar file is in the c:\SomeDir\AppDir directory.

But this does not work.
Here is the code that gets the resource:

java.net.URL imgURL =
ProjectorButton.class.getResource("discon.gif");
System.out.println("imgURL: " + imgURL);

The class ProjectorButton is in the package no.hials.ProjectorControl.gui.
The file "discon.gif" is placed at the root of the src-directory. When I use
NetBeans, the IDE creates a "build/classes" directory in which the
..class-files end up in a directorystructure simelar to the package
structure. The GIF-files are copied to the "build/classes" directory. When
run from Netbeans this works fine. NetBeans also builds a JAR from the files
in the "build/classes" directory, including the gif-files. The JAR file is
placed in a new directory, named "dist" which is at the same level as the
"build" directory. When I then open the CMD-window, and type:

.....\dist>java -cp ProjectorManager.jar
no.hials.ProjectorControl.gui.ProjectorControl

the application starts just fine, but the "imgURL" ends up beeing "null",
and I'm not able to understand why.

Any ideas, now that you've got some more details ? There must be something
I'm missing out on here....

-Arne


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Poll Markup. Where should I place the question? shapper HTML 3 10-08-2008 04:04 PM
Where should I place this code? shapper ASP .Net 1 07-14-2008 04:27 PM
how to change images based on action.Even clicking changed images should do respective actions? vj Javascript 3 01-12-2007 12:26 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
How should control images should be handled? ~~~ .NET Ed ~~~ ASP .Net Building Controls 1 11-03-2004 12:30 PM



Advertisments