0

This shows the icon when I launch through eclipse, but when I export it to a Runnable Jar It shows the default Java icon, I do not want to use the Resource way of doing it since it doesn't work in the IDE even.

public static void main(String args[]) {
    Game component = new Game();
    ImageIcon img = new ImageIcon("res/game.png");

    JFrame frame = new JFrame();
    frame.add(component);
    frame.setTitle(NAME);
    frame.setIconImage(img.getImage());
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    component.start();
}
BlueFoot
  • 15
  • 1
  • 5

3 Answers3

1

please use the following code

Image image = ImageIO.read(getClass().getResourceAsStream("/res/icon.png"));
setIconImage(new ImageIcon(image).getImage());

and put your icon file in res folder of src folder. But it'll display the icon of frame when you execute the jar file. Working fine for me.

ELITE
  • 5,815
  • 3
  • 19
  • 29
  • The code looks like try { Image image = ImageIO.read(Game.class.getResourceAsStream("/res/icon.png")); frame.setIconImage(new ImageIcon(image).getImage()); } catch (IOException e) { e.printStackTrace(); } But I get the following error Exception in thread "main" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source) at package.Game.main(Game.java:51) – BlueFoot May 09 '15 at 20:57
  • Problem was the path that I was using – BlueFoot May 10 '15 at 18:43
0

Try this following code to set the iconImage to the frame:

frame.setIconImage(new ImageIcon("res/game.png").getImage());

Even in this oracle doc you can find this same approach.

Edit: Have you tried this code. It might be helpful:

frame.setIconImage(new ImageIO.read(new File("res/game.png")));

Check out this explation too.

Community
  • 1
  • 1
Akash Rajbanshi
  • 1,553
  • 11
  • 23
0

The problem was that I was using the wrong run as file, a stupid mistake but thats what I get for not having my Workspace clean.

I'd like to thank everyone who tried to help.

BlueFoot
  • 15
  • 1
  • 5