Name: joT67522 Date: 08/26/97
Please try and prove to me that it works. I'm beginning to
think that including images within jar files only works with
applets, but not with APPLICATIONS.
GOAL: Make my java application (not applet) portable (PC or UNIX
anywhere) by including required images in a single jar file which also
contains all the class files. So, as long as the user's CLASSPATH
contained this JAR file, then everything should work, right? This would
be great if it worked. Then, you wouldn't have to worry about the PWD
problem that may change depending on where you start the app from.
The goal seems very simple and reasonable. But, I couldn't get this to
work! I wonder if the feature of including images in a JAR file was
only intended for Applets, and not applications. It never seems to load
the images from the jar file! The application works fine when I don't
try to access the images from the jar file.
DETAILS:
*I am using a single package that includes all classes (not that that
would matter).
*Here's how I'm obtaining the Image for use in my app:
gif1 Image =
Toolkit.getDefaultToolkit().getImage("Package-name/images/icon1.gif");
And I verified (w/ jar tvf jarfilename) that the jar file indeed
contained "Package-name/images/icon1.gif".
company - NRaD, code D4204, US Navy , email - ###@###.###
======================================================================
At the end of this description is a simple
program to display several Frames, each with an
image in it. When run as a class file with the
relevant GIFs in the same directory as it, it
works consistently. When run as a class file
in a JAR, with the GIF images in the JAR, the
following exception stack trace is printed about
one time in three:
Uncaught error fetching image:
java.lang.NullPointerException:
at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:82)
at sun.net.www.protocol.systemresource.SystemResourceManager.getFromZip(SystemResourceManager.java:128)
at sun.net.www.protocol.systemresource.SystemResourceManager.getLocalResource(SystemResourceManager.java:90)
at sun.net.www.protocol.systemresource.SystemResourceConnection.connect(SystemResourceConnection.java:67)
at sun.net.www.protocol.systemresource.SystemResourceConnection.getInputStream(SystemResourceConnection.java:87)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:97)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:259)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:151)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:127)
Reducing the number of images loaded seems to
decrease the probability of the problem occurring,
and increasing the number increases it.
This problem does not occur on SPARC/Solaris
systems. It does not seem to matter whether the
JAR is compressed or left uncompressed.
I can supply the images if required, but the
problem does not seem to depend on any particular
GIF.
--- Code
import java.awt.* ;
import java.awt.image.* ;
import java.net.URL ;
public class ImageCanvas extends Canvas {
private Image myImage = null ;
public ImageCanvas( Image i ) {
myImage = i ;
}
public void paint( Graphics g ) {
g.drawImage( myImage, 0, 0, this ) ;
}
public Dimension preferredSize() {
int width = myImage.getWidth( this ) ;
int height = myImage.getHeight( this ) ;
// Ensure the values are something sane
width = Math.max( width, 1 ) ;
height = Math.max( height, 1 ) ;
return new Dimension( width, height ) ;
}
public static Image loadImage( String filename ) {
URL u = ImageCanvas.class.getResource( "/" + filename ) ;
return Toolkit.getDefaultToolkit().getImage( u );
}
/**
* A main program to use the ImageCanvas to show a problem with
* image loading from JARs; if you run it on Windows NT from
* within a JAR file, 1 time in 3 or so it will bomb out with a
* NullPointerException in the image fetching thread. This seems
* to be timing-related; it's more likely to happen the first time
* you run it than later when the jar file is cached.
*/
public static void main( String args[] ) {
Image image = loadImage( "ru_RU.gif" ) ;
ImageCanvas c = new ImageCanvas( image ) ;
Frame f = new Frame( "ImageCanvas test" ) ;
f.add( "Center", c ) ;
f.pack() ;
f.show() ;
image = loadImage( "de_AT.gif" ) ;
c = new ImageCanvas( image ) ;
f = new Frame( "ImageCanvas test" ) ;
f.add( "Center", c ) ;
f.pack() ;
f.show() ;
image = loadImage( "tbCopy.gif" ) ;
c = new ImageCanvas( image ) ;
f = new Frame( "ImageCanvas test" ) ;
f.add( "Center", c ) ;
f.pack() ;
f.show() ;
image = loadImage( "tbProps.gif" ) ;
c = new ImageCanvas( image ) ;
f = new Frame( "ImageCanvas test" ) ;
f.add( "Center", c ) ;
f.pack() ;
f.show() ;
image = loadImage( "writable.gif" ) ;
c = new ImageCanvas( image ) ;
f = new Frame( "ImageCanvas test" ) ;
f.add( "Center", c ) ;
f.pack() ;
f.show() ;
}
}
- relates to
-
JDK-4124249 Regression: JDK1.1.6K classloading sometimes fails with "NoClassDefFoundError"
-
- Closed
-