Antonio Antonio - 4 months ago 10
Java Question

Images are loading in eclipse, but are not working in exported Jar

Title says it all, when I compile in eclipse it works fine, when I Export to a Jar file, they don't show up.

If you need anymore of the code, I will provide.

Image sprite = new ImageIcon("Sprites/Characters/spikeEnemy.png").getImage();

sprite = (e) ? new ImageIcon("Sprites/enemyProjectile.png").getImage() : new ImageIcon("Sprites/projectile.png").getImage();


(e: true for enemy, false for ally) sprite is declared earlier as (private Image sprite)

Answer

enter image description hereyou need to use getClassLoader: put the picture inside the src forlder

Image sprite = ImageIO.read(getClass().getClassLoader().getResource(path));

-> src /icons/icon.png

ImageIO.read(getClass().getClassLoader().getResource("icons/icon.png"));

this whay the image will be inside the executable jar