Name: dbT83986 Date: 05/13/99
When trying to load an ImageIcon from an application jar
file that resides on an unmapped network directory, the image
will not load. If you map the path to a drive instead of using
the UNC path, the image is able to be loaded.
Here is an example. I've included several different ways
of trying to display the image.
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.swing.border.*;
public class BugDemo extends JFrame
{
public BugDemo()
{
}
public Image getImageFromJAR(String fileName)
{
if( fileName == null ) return null;
Image image = null;
byte[] tn = null;
Toolkit toolkit = Toolkit.getDefaultToolkit();
InputStream in = getClass().getResourceAsStream(fileName);
try{
int length = in.available();
tn = new byte[length];
in.read( tn);
image = toolkit.createImage( tn);
}
catch(Exception exc){
System.out.println( exc +" getting resource " +fileName );
return null;
}
return image;
}
void method1()
{
JLabel label = new JLabel(new ImageIcon("images/Splash.GIF"));
label.setBorder(new BevelBorder(BevelBorder.LOWERED));
JOptionPane.showMessageDialog(this, label,
"About", JOptionPane.INFORMATION_MESSAGE);
}
void method2()
{
ImageIcon image =
new ImageIcon(ClassLoader.getSystemResource("images/Splash.GIF"));
JLabel label = new JLabel(image);
label.setBorder(new BevelBorder(BevelBorder.LOWERED));
JOptionPane.showMessageDialog(this, label,
"About2", JOptionPane.INFORMATION_MESSAGE);
}
void method3()
{
Image image = getImageFromJAR("images/Splash.GIF");
JLabel label = new JLabel(new ImageIcon(image));
label.setBorder(new BevelBorder(BevelBorder.LOWERED));
JOptionPane.showMessageDialog(this, label,
"About2", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args)
{
BugDemo frame = new BugDemo();
frame.method1();
frame.method2();
frame.method3();
System.exit(0);
}
}
method1 and method two do not return an image, while method
three results in a null pointer exception, when the application
is run from an unmapped directory.
(Review ID: 63144)
======================================================================