1

My application icon works fine when running the app from Eclipse, but once I export it as a runnable jar, then it doesn't appear.

This is the code I use to set the icon:

try {
    setIconImage(ImageIO.read(new File("resources/icons/icon.png")));
}
catch (IOException exc) {
    exc.printStackTrace();
}

The icon is in a source-folder called resources/icons.

Why doesn't the icon get included in the export?

Using URL now instead of looking for the icon with the File method. This works great!

Is it possible to change - by default - the file's icon? I think I have to use resource hacker or something like that in order to change it. But if it's possible via code, I'd love to learn it!

Jonathan
  • 685
  • 1
  • 10
  • 30
  • Did you insert the folder in your build or your export specification? – IQV Jan 30 '17 at 08:52
  • 1
    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 [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jan 30 '17 at 08:57
  • @IQV it's not an option, it seems like, in eclipse when exporting as a runnable jar! – Jonathan Jan 30 '17 at 10:09
  • 1
    @AndrewThompson this seems quite interesting, will look at this – Jonathan Jan 30 '17 at 10:09
  • When you export your jar, you click once "Next" in the wizard. The second window shows the "resources to export". When you open the tree of your project, is the resource-folder checked? – IQV Jan 30 '17 at 10:14
  • @IQV I don't have that window. This is what I get after pressing next on Runnable Jar: https://gyazo.com/655c2bbee790389f2ba2478491fdccc7 – Jonathan Jan 30 '17 at 10:16
  • You get this window, when you use "JAR file" instead of "Runnable JAR file". Unfortunately I always use the first. I have never used the runnable jar file. Sorry. – IQV Jan 30 '17 at 10:25
  • @IQV doesn't work for me if it's not a runnable jar file, the app doesn't open :/ But I managed to solve it by using a URL anyways! – Jonathan Jan 30 '17 at 10:35
  • Possible duplicate of [How to load icon from resource in Java?](http://stackoverflow.com/questions/8660945/how-to-load-icon-from-resource-in-java) – Divyesh Kanzariya Jan 30 '17 at 11:08
  • 1
    Possible duplicate of [Trying to load icon from jar file](http://stackoverflow.com/questions/1133066/trying-to-load-icon-from-jar-file) – toesslab Jan 30 '17 at 17:57

1 Answers1

1

You can use getResource instead of new File(), like this:

Icon icon = new ImageIcon(getClass().getResource("resources/icons/icon.png"));

So your setIconImage should be like so:

setIconImage(new ImageIcon(getClass().getResource("resources/icons/icon.png")).getImage());

If your icon exit inside your package you can do like this:

Icon icon = new ImageIcon(getClass().getResource("/com/icons/icon.png"));

So your setIconImage should be like so :

setIconImage(new ImageIcon(getClass().getResource("/com/icons/icon.png")).getImage());

Hope this helped.

Jonathan
  • 685
  • 1
  • 10
  • 30
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 1
    *If your icon exit inside your package you can do like so: .. `"/com/icons/icon.png"`" Actually, then it can be just `"icon.png"`whereas ``"com/icons/icon.png"`* is for a path **relative to the current package** (would not recommend) and `"/com/icons/icon.png"` is a path from the root of the class-path (most robust). – Andrew Thompson Jan 30 '17 at 09:07