1

I'm working on a java project and made it into an executable jar. In the jar, all the correct files are included, but when running the jar, the images don't display.

When I run the program from command line, the program is able to display everything correctly.

I believe the issue might be because of how I set up the filepaths in the code? Here's an example of my setup:

private static String imgPath;
...
    imgPath = String.format("img%d.gif", value);
...
public static ImageIcon getImageIcon() {
    ImageIcon ii = new ImageIcon("content/dice/" + imgPath);
    return ii;
}
//getImageIcon() is later called by another class

This setup works unless I try to run the program from an executable jar. So my question is how do I get it to work from a jar?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
YianniW
  • 57
  • 5

1 Answers1

3

The first mistake is assuming there is a file system (directories & such) inside a Jar. There are paths to resources that might look like directories & files, but no directories or files as such.

Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433