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 / May 2008

Tip: Looking for answers? Try searching our database.

images and jar file craziness

Thread view: 
Redbeard - 06 May 2008 04:01 GMT
I've got an app that uses images that will be used with a
JToggleButton.  I started out with
Icon myIcon = new ImageIcon("images\\myImage.jpg");

Now I want to wrap all of it up in an executable jar file.  I've done
a ton of research, visited and revisited Roedy's site, and nothing
I've tried seems to work.

I've tried using the Toolkit, URL's, etc..  The jar will run, but it
will only read the images from hard drive, not from within the jar
file.  So if I move the jar file, I get no images.

I've also tried
getClass().getResource() and the jar file won't even open

BTW, the class that needs the Icon's is a subclass of JPanel.

Any other ideas?

TIA
Lew - 06 May 2008 04:45 GMT
> I've got an app that uses images that will be used with a
> JToggleButton.  I started out with
> Icon myIcon = new ImageIcon("images\\myImage.jpg");

Use forward slashes, and newImageIcon( getClass().getResource(
"images/myImage.jpg" ));

> I've also tried
> getClass().getResource() and the jar file won't even open

What do you mean the "jar [sic] file won't open"?  Are you trying to open the
JAR file?  You should be opening the image file.

You say you're using the method, but you don't show us *how* you're using it.
  The problem is in how you're using the method.

To give help, we must have enough information.
<http://sscce.org/>

Signature

Lew

cowderyt@district87.org - 06 May 2008 14:03 GMT
> > I've got an app that uses images that will be used with a
> > JToggleButton.  I started out with
[quoted text clipped - 17 lines]
> --
> Lew

What I mean when I say that the jar file won't open is that when I
double-click on it, it will not execute.  I always make sure that the
program runs BEFORE I jar it all up.  But I can't get things to run
properly - and sometimes not at all - from the jar.

For example, when I use the following line, the program runs fine when
the images folder is in the same folder as the jar file.  But when I
move the jar file, it opens, but doesn't have the image.  That tells
me that it is NOT reading from the image folder inside the jar.
myCardBack = new ImageIcon("images/background.jpg");

When I change it to
myCardBack = new ImageIcon(getClass().getResource("images/
background.jpg"));
The jar file won't even execute.  Actually, I suspect that it may
execute, but is hanging up somewhere before anything becomes visible.
My suspicion is based on the fact that I can't delete the jar file.  I
get an error indicating that it is "in use".  Again, the program runs
fine when it is NOT jarred.

I've also tried
myCardBack = new
ImageIcon(Toolkit.getDefaultToolkit().getImage("images/
background.jpg"));
Same result - the jar file either will not execute or is hanging up
somewhere.  Program works fine un-jarred.
Nigel Wade - 06 May 2008 15:26 GMT
>> > I've got an app that uses images that will be used with a
>> > JToggleButton.  I started out with
[quoted text clipped - 28 lines]
> me that it is NOT reading from the image folder inside the jar.
> myCardBack = new ImageIcon("images/background.jpg");

No, that won't load from the jar. It is explicitly loading a file called
images/background.jpg relative to the current directory in the filesystem, not
the jar.

> When I change it to
> myCardBack = new ImageIcon(getClass().getResource("images/
[quoted text clipped - 4 lines]
> get an error indicating that it is "in use".  Again, the program runs
> fine when it is NOT jarred.

What is most likely happening is that your jar is crashing due to throwing an
uncaught NPE. If the image cannot be loaded myCardBack will be null.

Don't forget that "images/" is *relative* to whatever getClass() returns. This
is the current class and so the images directory it tries to locate will be in
the directory from which the current class file was loaded. If you want a
top-level images directory, at the root of the jar, then use an absolute file
path "/images" rather than a relative one.

> I've also tried
> myCardBack = new
> ImageIcon(Toolkit.getDefaultToolkit().getImage("images/
> background.jpg"));
> Same result - the jar file either will not execute or is hanging up
> somewhere.  Program works fine un-jarred.

Signature

Nigel Wade

cowderyt@district87.org - 06 May 2008 19:54 GMT
> Don't forget that "images/" is *relative* to whatever getClass() returns. This
> is the current class and so the images directory it tries to locate will be in
> the directory from which the current class file was loaded. If you want a
> top-level images directory, at the root of the jar, then use an absolute file
> path "/images" rather than a relative one.

That did the trick!!!

Thanks!!!
cowderyt@district87.org - 06 May 2008 20:00 GMT
On May 6, 1:54 pm, cowde...@district87.org wrote:

> > Don't forget that "images/" is *relative* to whatever getClass() returns. This
> > is the current class and so the images directory it tries to locate will be in
[quoted text clipped - 5 lines]
>
> Thanks!!!

PS.  I'm still "Redbeard", the OP.  The address is different because I
forgot that I was already logged in to Google using my "work" ID.
Sorry for any confusion!
Roedy Green - 06 May 2008 17:35 GMT
>What I mean when I say that the jar file won't open is that when I
>double-click on it, it will not execute.  I always make sure that the
>program runs BEFORE I jar it all up.  But I can't get things to run
>properly - and sometimes not at all - from the jar.

Learn to partition your problems. Tackle a simpler program first to
learn how to get double click working.

http://mindprod.com/jgloss/jar.html#ASSOCIATION
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

cowderyt@district87.org - 06 May 2008 19:41 GMT
On May 6, 11:35 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Tue, 6 May 2008 06:03:56 -0700 (PDT), cowde...@district87.org
> wrote, quoted or indirectly quoted someone who said :
[quoted text clipped - 12 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

While I'm certainly no expert, I'm also not a novice.  I've written
several apps and gotten them to work in a jar, but none had images in
them.  And I can get this app to run from the jar (depending on what
code I have used), but I can't get the images to work.

I'll try what you and Nigel have suggested to see if that helps.

Thanks!
Roedy Green - 07 May 2008 05:02 GMT
>While I'm certainly no expert, I'm also not a novice.  I've written
>several apps and gotten them to work in a jar, but none had images in
>them.  And I can get this app to run from the jar (depending on what
>code I have used), but I can't get the images to work.

I understood from you earlier post you could not get a jar to execute
at all.  Apparently it executed but crapped out as soon as it came
time to deal with the image.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Redbeard - 08 May 2008 02:54 GMT
On May 6, 11:02 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Tue, 6 May 2008 11:41:07 -0700 (PDT), cowde...@district87.org
> wrote, quoted or indirectly quoted someone who said :
[quoted text clipped - 11 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

Sorry for the confusion.  Apparently, I wasn't as clear as I thought I
was.

Before I posted, I had researched and tried a variety of different
ways to load the images.  They all worked from the class files, but
none would load the images once I jarred everything up.  Most of the
time the jar file would execute, but not have images.  However, SOME
techniques actually kept the jar file from executing - or more likely
- caused it to hang before anything became visible.

I was sure I was making some small mistake that I just couldn't see -
which is why I posted.  I see now that my problem was that I was using
the relative path rather than the absolute path.  Obviously, I have
more to learn about that.  I'd read about absolute vs. relative paths
and thought that relative was the way to go.
Roedy Green - 08 May 2008 19:23 GMT
On Wed, 7 May 2008 18:54:36 -0700 (PDT), Redbeard
<tom.cowdery@bigfoot.com> wrote, quoted or indirectly quoted someone
who said :

>I was sure I was making some small mistake that I just couldn't see -
>which is why I posted.  I see now that my problem was that I was using
>the relative path rather than the absolute path.  Obviously, I have
>more to learn about that.  I'd read about absolute vs. relative paths
>and thought that relative was the way to go.

To get at resources inside the jar you use either the resource name
prefixed by the package or without where the package is implied by the
package of the calling class.

I explain this at http://mindprod.com/jgloss/resource.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Nigel Wade - 09 May 2008 09:48 GMT
> On May 6, 11:02 pm, Roedy Green <see_webs...@mindprod.com.invalid>
> wrote:
[quoted text clipped - 29 lines]
> more to learn about that.  I'd read about absolute vs. relative paths
> and thought that relative was the way to go.

It's not that you were using a relative path, relative paths are perfectly ok
and it may be argued that they are the preferred method. The problem is most
likely that you were using the wrong relative path. There is only one absolute
path, and that path is independent of the object you use as the basis of the
getClass().getResource(). There are a large number of relative paths you might
use, and each is dependent on where you base the relative path.

For a relative path the origin is the directory containing the class which is
returned by getClass(). You need to base your relative path in getResource()
from that directory. Don't forget that the jar has a directory structure based
on the package structure of the class definitions.

Signature

Nigel Wade

Redbeard - 12 May 2008 03:27 GMT
> > On May 6, 11:02 pm, Roedy Green <see_webs...@mindprod.com.invalid>
> > wrote:
[quoted text clipped - 44 lines]
> --
> Nigel Wade

In my case, I only had three files, so I hadn't bothered to create a
package.  I have used packages in other projects, but primarily when
creating libraries for use by others, not is a small project like
this.  My source code and compiled files all ended up in the same
folder (directory) G:\Javasource\MemoryGame and the images were in G:
\Javasource\MemoryGame\images.

Do you have to use packages to get a relative path to work?
Lew - 12 May 2008 04:04 GMT
> In my case, I only had three files, so I hadn't bothered to create a
> package.  I have used packages in other projects, but primarily when
[quoted text clipped - 4 lines]
>
> Do you have to use packages to get a relative path to work?

Give it a try and see.

Signature

Lew

Roedy Green - 06 May 2008 17:33 GMT
On Mon, 5 May 2008 20:01:07 -0700 (PDT), Redbeard
<tom.cowdery@bigfoot.com> wrote, quoted or indirectly quoted someone
who said :

>Now I want to wrap all of it up in an executable jar file.  I've done
>a ton of research, visited and revisited Roedy's site, and nothing
>I've tried seems to work.

see http://mindprod.com/jgloss/image.html
http://mindprod.com/jgloss/resource.html
http://mindprod.com/jgloss/imageicon.html

Just take the sample code under ImageIcon and prove to yourself it
works.  The main trick is building the jar properly so that you have
an image in the jar named:

com/mindprod/example/raspberry.png

see http://mindprod.com/jgloss/jar.html

Then with that under your belt, I think you should have no trouble
getting it to work in your own program.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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.