Eclipse Exported Runnable Jar Not Showing Images

Eclipse exported Runnable JAR not showing images

The problem was I had this project in my Windows profile... that had an "!" in it... (DeNitE! -> was the name of my Windows profile)

As soon as I changed it to DeNitE (without the !) it worked fine...

Images loading in exported JAR, but not when running from eclipse

After a long while, I finally discovered what the issue was.

In my exported JAR (a little game library I was then importing into my game project), I have a class for loading images that used

getClass().getResourceAsStream(path)

given path was a string to a real image. This worked fine when running test classes actually inside my game library project because I guess getResourceAsStream() uses a localised root being the root of the game library project.

So, my fix was to create two constructors for my image loading class (in the game library). One that still accepted file path strings and used getClass().getResourceAsStream(path), like so:

public Image(String path)
{
loadImageResource(getClass().getResourceAsStream(path));
}

private void loadImageResource(InputStream path)
{
try {
image = ImageIO.read(path);
}
catch (IOException e) {
e.printStackTrace();
}
}

and another for projects using the game library as an exported JAR, that accepted an InputStream object. This means that the method calling the Image constructor will need to run getClass().getResourceAsStream(path) itself (I guess then using that project as the root, rather than the exported JAR's root). Like so:

public Image(InputStream path)
{
loadImageResource(path);
}

private void loadImageResource(InputStream path)
{
try {
image = ImageIO.read(path);
}
catch (IOException e) {
e.printStackTrace();
}
}

Exporting java project to runnable jar file with images

The problem is not with creating the jar file, it is with the way you are reading the image, do read the image in your java code as below it will work.

ImageIO.read( ClassLoader.getSystemResource( "image/button1.png" ) );

Exported JAR file does not show images

I think I found the fix:

Replace this

new ImageIcon("res/settings.gif");

with

new ImageIcon(this.getClass().getResource("/settings.gif"));

Works both from Eclipse and from a runnable JAR file.

Image won't show up when using JAR file

Change it:

image = ImageIO.read(getClass().getResource("/images/Chess_Pieces.png"));

See :

  • Different ways of loading a file as an InputStream

  • Loading image resource



Related Topics



Leave a reply



Submit