For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2006 > in or out of jar, how does java.net.URL and getResource decide?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2006-01-26, 7:06 pm

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.



--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Arnaud B.

2006-01-27, 3:59 am

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


"Kent Paul Dolan" <xanthian@well.com> a écrit dans le message de
news:4bddde854e0fe9d9f543cf1995b7631b.48257@mygate.mailgate.org...
> 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_B
UTTON])
> {
>
> 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/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
> 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.
>
>
>
> --
> Posted via Mailgate.ORG Server - http://www.Mailgate.ORG



Kent Paul Dolan

2006-01-27, 7:03 pm

Arnaud B. wrote:

> KPD wrote:


[color=darkred]
[color=darkred]
> 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.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com