1

In my project I use the system tray, when I compile the program everything is working fine and the icon that I use for the system tray shows up.

The icon is placed in the project folder and the code related to the icon is

Image icon = Toolkit.getDefaultToolkit().getImage("Icon.png");

trayIcon = new TrayIcon(icon, "Program name", popup);
trayIcon.setImageAutoSize(true);

tray.add(trayIcon);

As I said, everything works find but when I export the project as a runnable jar the program will run but the icon will not show up, but it will just be blank.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2526311
  • 1,004
  • 2
  • 10
  • 19
  • Make sure you include it in the list of files, but then use a resource loader from the class to load the image. –  Jul 08 '13 at 21:01
  • I hope this [answer](http://stackoverflow.com/a/9866659/1057230) might be of some use for this situation – nIcE cOw Jul 09 '13 at 13:19

2 Answers2

6

If you would like to load resources from your .jar file use getClass().getResource(). That returns a URL with correct path.

Image icon = ImageIO.read(getClass().getResource("image´s path"));
DRastislav
  • 1,892
  • 3
  • 26
  • 40
5

To access images in a jar, use Class.getResource().

I typically do something like this:

InputStream stream = MyClass.class.getResourceAsStream("Icon.png");
if(stream == null) {
   throw new RuntimeException("Icon.png not found.");
}

try {
   return ImageIO.read(stream);
} catch (IOException e) {
   throw new RuntimeException(e);
} finally {
   try {
      stream.close();
   } catch(IOException e) { }
}
Russell Zahniser
  • 16,188
  • 39
  • 30