Search code examples
javaimageswingresourcessystem-tray

images are not displaying in executable jar


i have created executable jar file of my project built in eclipse. but when i execute that file then it does not display icon on system tray that i have added in project. i am using following simple code.

Image image = Toolkit.getDefaultToolkit().getImage("src/resources/ChatIcon1.jpeg");
PopupMenu Popup = new PopupMenu();
MenuItem exit = new MenuItem("Exit");
Popup.add(exit);

final TrayIcon trayIcon = new TrayIcon(image,"OfficeCommunicator",Popup);
trayIcon.setImageAutoSize(true);

Solution

  • To load resources from within .jar files please use getClass().getResource(). That will return a URL with correct path.

    Image img = ImageIO.read(getClass().getResource("path to image"));