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
Java 8: Difference Between Two Localdatetime in Multiple Units
Calling a Subclass Method from Superclass
How to Set the Default Locale in the Jvm
Cut Out Image in Shape of Text
Scale the Imageicon Automatically to Label Size
Update Jlabel Every X Seconds from Arraylist<List> - Java
Differences Between Oracle Jdk and Openjdk
Implementing Singleton with an Enum (In Java)
How to Construct a Relative Path in Java from Two Absolute Paths (Or Urls)
Java, Classpath, Classloading => Multiple Versions of the Same Jar/Project
Integer Wrapper Objects Share the Same Instances Only Within the Value 127