Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: images and ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 15 Topic 16051 of 16078
Post > Topic >>

Re: images and jar file craziness

by Nigel Wade <nmw@[EMAIL PROTECTED] > May 6, 2008 at 03:26 PM

cowderyt@[EMAIL PROTECTED]
 wrote:

> On May 5, 10:45 pm, Lew <l...@[EMAIL PROTECTED]
> wrote:
>> Redbeard wrote:
>> > 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/>
>>
>> --
>> 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");

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/
> 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.

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.

-- 
Nigel Wade




 15 Posts in Topic:
images and jar file craziness
Redbeard <tom.cowdery@  2008-05-05 20:01:07 
Re: images and jar file craziness
Lew <lew@[EMAIL PROTEC  2008-05-05 23:45:53 
Re: images and jar file craziness
cowderyt@[EMAIL PROTECTED  2008-05-06 06:03:56 
Re: images and jar file craziness
Nigel Wade <nmw@[EMAIL  2008-05-06 15:26:45 
Re: images and jar file craziness
Roedy Green <see_websi  2008-05-06 16:35:16 
Re: images and jar file craziness
Roedy Green <see_websi  2008-05-06 16:33:15 
Re: images and jar file craziness
cowderyt@[EMAIL PROTECTED  2008-05-06 11:41:07 
Re: images and jar file craziness
Roedy Green <see_websi  2008-05-07 04:02:14 
Re: images and jar file craziness
cowderyt@[EMAIL PROTECTED  2008-05-06 11:54:47 
Re: images and jar file craziness
cowderyt@[EMAIL PROTECTED  2008-05-06 12:00:41 
Re: images and jar file craziness
Redbeard <tom.cowdery@  2008-05-07 18:54:36 
Re: images and jar file craziness
Roedy Green <see_websi  2008-05-08 18:23:56 
Re: images and jar file craziness
Nigel Wade <nmw@[EMAIL  2008-05-09 09:48:24 
Re: images and jar file craziness
Redbeard <tom.cowdery@  2008-05-11 19:27:53 
Re: images and jar file craziness
Lew <lew@[EMAIL PROTEC  2008-05-11 23:04:30 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 9:29:10 CDT 2008.