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 / GUI / July 2005

Tip: Looking for answers? Try searching our database.

gif and jpg files missing in gui of application run from jar file

Thread view: 
Mark - 15 Jul 2005 01:59 GMT
I have packaged a Java application into a jar file, along with some gif
and jpg files that appear in the application, but the gif and jpg files
do not appear in the application gui when I run it from the jar file.

Starting out, the class, gif, and jpg files are in the same directory,
and the class files are not in a package (no package statement in the
java source).

An example of the code I use to get the images into the application are
as follows:

  private ImageIcon image1 = new ImageIcon("image1.jpg");

-OR-

  private ImageIcon image1 = new ImageIcon("./image1.jpg");

When running the application from the command prompt, either technique
works fine - the image appears in the application:

java MyClass

I jar up the class, gif, and jpg files.  I check the jar file to make
sure things are there:

jar -tvf MyJar.jar

...and the class, gif, and jpg files are all there, in the same
directory in the jar file.  The case of the file names is the same in
the original development directory, in the application source, and in
the jar file.

However, when I run the application from the jar, the gif and jpg files
do not appear in the gui:

java -jar MyJar.jar

Any ideas??

I'm using Java 1.5.0 on a Windows 2000 system.

Regards,

Mark
Andrew Thompson - 15 Jul 2005 03:50 GMT
> I have packaged a Java application into a jar file, along with some gif
> and jpg files that appear in the application, but the gif and jpg files
[quoted text clipped - 3 lines]
> and the class files are not in a package (no package statement in the
> java source).
..
> Any ideas??

Put your classes is a package.  I suspect that will
solve the problem.  As far as I understand, the
*intention* was to always have /packaged/ classes in
archives, anything that works for a default package
class might as well be considered 'pure good luck'.

I can provide no guarantee that putting the classes
in a package will solve the problem, but I feel it
is worth a try.

HTH

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Where No Fan Has Gone Before

Tom N - 15 Jul 2005 04:08 GMT
>> I have packaged a Java application into a jar file, along with some gif
>> and jpg files that appear in the application, but the gif and jpg files
[quoted text clipped - 15 lines]
> in a package will solve the problem, but I feel it
> is worth a try.

If the image files are in the same package as the class accessing them, then do this.

ImageIcon image = new ImageIcon(this.getClass().getResource("test.png"));
Mark - 15 Jul 2005 04:19 GMT
>>>I have packaged a Java application into a jar file, along with some gif
>>>and jpg files that appear in the application, but the gif and jpg files
[quoted text clipped - 21 lines]
>
> ImageIcon image = new ImageIcon(this.getClass().getResource("test.png"));

I've found the solution you recommend in Sun's Java forums as well.
I'll try it tomorrow and post the results here.  Any idea why packaging
thngs into a jar requires this particular bit of coding?

Regards,

Mark
Andrew Thompson - 15 Jul 2005 07:34 GMT
>> If the image files are in the same package as the class accessing them, then do this.
>>
>> ImageIcon image = new ImageIcon(this.getClass().getResource("test.png"));
>
> I've found the solution you recommend in Sun's Java forums as well.

Where?  URL?

I doubt it was stated like that, since that provides little
more information, than the way that you were doing it.

In fact, 'under the covers' that is exactly what the String
constructor is doing..
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/ImageIcon.html#ImageIcon(jav
a.lang.String
)>

The only advantage to doing it that way comes if you
split it up further.

 URL imageURL = this.getClass().getResource(pathString);
 System.out.println( "imageURL: " + imageURL);
 ImageIcon image = new ImageIcon( imageURL );

WSe are all *expecting* imageURL to be 'null', though
if it prints an URL - that is entirely another matter!

> I'll try it tomorrow and post the results here.  Any idea why packaging
> thngs into a jar requires this particular bit of coding?

Using getResource() consistently is the best way to get your
resources, most of the methods and constructors that take strings
or URL's, actually use getResource behind the scenes to convert
the string to an URL.  

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
See You On Some Other Channel

Mark - 15 Jul 2005 11:48 GMT
>>>If the image files are in the same package as the class accessing them, then do this.
>>>
[quoted text clipped - 28 lines]
> or URL's, actually use getResource behind the scenes to convert
> the string to an URL.  

Andrew and Tom...

Thanks to you both.  The solution suggested by...

ImageIcon image =
new ImageIcon(this.getClass().getResource("test.png"));

...does the trick.  (No need to put my classes into packages.)

The Sun Java forum entries that seemed to suggest something similar were...

http://forum.java.sun.com/thread.jspa?threadID=643547&tstart=0

http://forum.java.sun.com/thread.jspa?threadID=641056&tstart=30

http://forum.java.sun.com/thread.jspa?threadID=621960&tstart=165

http://forum.java.sun.com/thread.jspa?threadID=610633&tstart=195

Regards,

Mark
Tom N - 15 Jul 2005 15:13 GMT
>>> If the image files are in the same package as the class accessing
>>> them, then do this.
[quoted text clipped - 5 lines]
>
> Where?  URL?

Sun's How to Use Icons tutorial says essentially the same thing.
http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html

> I doubt it was stated like that, since that provides little
> more information, than the way that you were doing it.
[quoted text clipped - 13 lines]
> WSe are all *expecting* imageURL to be 'null', though
> if it prints an URL - that is entirely another matter!

Huh?  URL is obviously not null or there's not going to be any image icon.
URL will be something like file:/C:/temp/x2.png


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.