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 / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

in or out of jar, how does java.net.URL and getResource decide?

Thread view: 
Kent Paul Dolan - 26 Jan 2006 21:49 GMT
I've got a kuhl application that among other things, for a demo,
flashes pictures, meant to look like a top-down view of a map,
based on two image data sets stored in the jar file. The call to
find them, and alternate them in a JPanel is done like this:

 public void actionPerformed(ActionEvent ae)
 {
   java.net.URL imgURL;

   if ( ae.getSource() ==

EvwfConstantButtons.buttonElementArray[EvwfConstantButtons.PBC_REFRESH_MAP_BUTTON])
   {

     if ( maptoggle )
     {
       imgURL = EvwfUtilities.class.getResource
       (
         "images/EvwfSampleMap1.png" // FIXME Obviously we don't want
this hard wired
       );
     }
     else
     {
       imgURL = EvwfUtilities.class.getResource
       (
         "images/EvwfSampleMap2.png" // FIXME Obviously we don't want
this hard wired
       );
     }

     maptoggle = maptoggle ? false : true ;

     Image img = getToolkit().getImage(imgURL);

     System.err.print( "Refreshed Map URL is: " + imgURL.toString() +
"\n" );
     try {
         MediaTracker tracker = new MediaTracker(this);
         tracker.addImage(img, 0);
         tracker.waitForID(0);
     } catch (Exception e) {}

     int iw = img.getWidth(this);
     int ih = img.getHeight(this);
     bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
     Graphics2D big = bi.createGraphics();
     big.drawImage(img,0,0,this);
     this.repaint();

   }
 }

which works fine, the two faked up maps are in the Jar at the
correct relative location to the package's directory, and all
is well.

The trace of the call shows that java.net.URL found my data
sets at:

Refreshed Map URL is:

jar:file:/home/rooty/java/EastValleyWaterForumInterface/evwfLocal.jar!/edu/asu/dt/evwf/images/EvwfSampleMap2.png
Refreshed Map URL is:

jar:file:/home/rooty/java/EastValleyWaterForumInterface/evwfLocal.jar!/edu/asu/dt/evwf/images/EvwfSampleMap1.png

The same code, running not from the jar file, found them here, in
normal, non-jar-archive relative file path names, converted to full
ones, instead:

Refreshed Map URL is:

file:/home/rooty/java/EastValleyWaterForumInterface/edu/asu/dt/evwf/images/EvwfSampleMap2.png
Refreshed Map URL is:

file:/home/rooty/java/EastValleyWaterForumInterface/edu/asu/dt/evwf/images/EvwfSampleMap1.png

But now, I want to _run from_ the jar, but _access files_ (the real
map data this time, being updated by an independent process from
outside my control) from a non-jar directory relative to the one in
which the jar file sits.

How do I convince java.net.URL (or more correctly the
EvwfUtilities.class.getResource( String dataRelativeLocation )
calls) to do that?

xanthian.
Arnaud B. - 27 Jan 2006 08:41 GMT
Hi,

getResource will find the files provided they are inside a jar in the
classpath, or a directory in the classpath.

You don't have to tell getResource whether it should look for a jar archive
or in directories.
It will return the first one it finds in its classpath .

Whether you run your app from a jar or not, makes no difference to this.

You just have to ensure that a given resource (with its relative path)
cannot be found at two different places in the classpath (say, remove those
from the jar to be sure that the filesystem's ones will be found).

Arnaud

> I've got a kuhl application that among other things, for a demo,
> flashes pictures, meant to look like a top-down view of a map,
[quoted text clipped - 6 lines]
>
>     if ( ae.getSource() ==

EvwfConstantButtons.buttonElementArray[EvwfConstantButtons.PBC_REFRESH_MAP_B
UTTON])
>     {
>
[quoted text clipped - 45 lines]
>
> Refreshed Map URL is:

jar:file:/home/rooty/java/EastValleyWaterForumInterface/evwfLocal.jar!/edu/a
su/dt/evwf/images/EvwfSampleMap2.png
> Refreshed Map URL is:

jar:file:/home/rooty/java/EastValleyWaterForumInterface/evwfLocal.jar!/edu/a
su/dt/evwf/images/EvwfSampleMap1.png

> The same code, running not from the jar file, found them here, in
> normal, non-jar-archive relative file path names, converted to full
> ones, instead:
>
> Refreshed Map URL is:

file:/home/rooty/java/EastValleyWaterForumInterface/edu/asu/dt/evwf/images/EvwfSampleMap2.png
> Refreshed Map URL is:

file:/home/rooty/java/EastValleyWaterForumInterface/edu/asu/dt/evwf/images/EvwfSampleMap1.png

> But now, I want to _run from_ the jar, but _access files_ (the real
> map data this time, being updated by an independent process from
[quoted text clipped - 6 lines]
>
> xanthian.
Kent Paul Dolan - 27 Jan 2006 18:18 GMT
> KPD wrote:

>> But now, I want to _run from_ the jar, but _access files_ (the real
>> map data this time, being updated by an independent process from
>> outside my control) from a non-jar directory relative to the one in
>> which the jar file sits.

>> How do I convince java.net.URL (or more correctly the
>> EvwfUtilities.class.getResource( String dataRelativeLocation )
>> calls) to do that?

> getResource will find the files provided they are inside a jar in the
> classpath, or a directory in the classpath.

> You don't have to tell getResource whether it should look for a jar archive
> or in directories.
> It will return the first one it finds in its classpath .

> Whether you run your app from a jar or not, makes no difference to this.

> You just have to ensure that a given resource (with its relative path)
> cannot be found at two different places in the classpath (say, remove those
> from the jar to be sure that the filesystem's ones will be found).

Ah, the insights one fails to gain from laziness. I've now written
megabytes
of Java source code without ever once assigning a classpath. So, just
avoiding
namespace collisions in what getResource treats as an inherently
ambiguous
file system between that internal to a jar and a possible (very likely,
really)
identical filesystem directory layout parallel with that within the jar
does the trick!

Who'd have guessed!?!

Thanks for the help!

xanthian.


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.