3

I am developing a java project in eclipse. In order to distribute the program I have created a runnable .jar-file via the export-function in eclipse.

In my program a load several images that are stored in a folder called 'tableImages'. I load the images via the ClassLoader (you'll find the code snippets below). The problem is the following: When executing the program from the .jar-file, it throws a NullPointerException when loading one of the .png-files in the above mentioned folder. Now the funny thing is, that some of the .png-files in the exact same folder are loaded correctly, meaning they don't cause a NullPointerException.

I checked the content of the .jar using the jar tf command. The image that apparently cannot be loaded, was packed into the jar. So what is causing this NullPointerException and how can I solve it?

Here is the class with which I draw images to my swing-frame.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.net.*;

public class LoadBackgroundImage extends Component {

    private static final long serialVersionUID = 1L;

    BufferedImage img;

    public void paint(Graphics g){
        g.drawImage(img, 0, 0, null);
    }

    public LoadBackgroundImage(String image){
        URL url = ClassLoader.getSystemResource(image);
        try{
            img = ImageIO.read(url);
            this.setSize(img.getWidth(), img.getHeight());
        }catch(IOException e){
            System.err.println(e.getMessage());
        }
    }

    public Dimension getPreferredSize(){
        if(img == null){
            return new Dimension(100, 100);
        }else{
            return new Dimension(img.getWidth(null), img.getHeight(null));
        }
    }

    public BufferedImage getImage(){
          return img;
         }

}

Thank you very much for your help!

kafman
  • 2,862
  • 1
  • 29
  • 51
  • 4
    Could you also edit in the exception thrown and it's stracktrace/log message? It's very helpful for finding errors :) – nuala May 12 '12 at 21:38
  • 1
    Check this [HOW TO ADD IMAGES TO YOUR PROJECT IN ECLIPSE](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659) or you can follow the steps mentioned [here](http://gagandeepbali.uk.to/gaganisonline/webpages/makejareclipse.html) – nIcE cOw May 13 '12 at 02:00
  • Thank you, this link was helpful, too! – kafman May 13 '12 at 13:14

3 Answers3

3
URL url = ClassLoader.getSystemResource(image);

This URL translates to absolute path in the file system which will be an invalid path for a file within jar. For example, if you have a file called foo.png under bar.jar the URL will be /path_to_current_dir/bar.jar/!foo.png which is invalid. Instead you can read the file as a stream:

InputStream is = getClass().getClassLoader().getResourceAsStream(image);
ImageIO.read(is);
Prasanna
  • 3,703
  • 9
  • 46
  • 74
2

Assuming that the file is indeed in the .jar package, the reason is most likely that entries in .jar-files are loaded in a case sensitive matter so that foo.png is not the same as Foo.png. When you load from regular files the behavior depends on the file system so on Windows and Mac foo.png and Foo.png will denote the same file(!).

Mathias Schwarz
  • 7,099
  • 23
  • 28
2

You can use a URL, I just tried it myself and it works perfectly, getting the png files from within the jar:

Here's the code I used:

public static ImageIcon getIcon(String path)
    {
        String imageFile = "";

        if(path.equals(""))
            imageFile = "/Images/Icon.png";
        else
            imageFile = path;

        URL source = Messages.class.getResource(imageFile);
        if(source != null)
        {
            ImageIcon imageIcon = new ImageIcon(source);
            return imageIcon;
        }
        return null;
    }

Then I add it directly to a JLabel, worked like a charm, and I also moved the jar file around and renamed the imagefiles outside of the jar, so I'm sure.

Martin
  • 1,130
  • 10
  • 14
  • This, on the other hand did not work btw: new ImageIcon(Toolkit.getDefaultToolkit().getImage(path)); – Martin May 12 '12 at 22:22