I’m trying to build an application that shows an icon in the system tray, this icon will have multiple functions.
The application is in it’s early stages so i just picked up a random image from internet(a green dot), copied in my “/resource/assets” folder and it works perfectly if i run the application from Intellij (or any other IDE).
The problem occurs when I try to build the application using maven (just a simple clean install), the application runs fine, no errors are displayed, but it seems like it can’t retrieve the image that has to be shown as an icon in the system tray. A placeholder is shown instead, all the functions works.
I tried to:
- Tell maven to include all files in the resources dir
like this:
<build>
<resources>
<resource>
<directory>src/main/resources/assets</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
- Put the image in the same folder as the .jar file and tell the application that the file is in the same directory
like this:
private static TrayIcon createTrayIconFromFile() {
logger.info("Creating tray icon from file");
String userDir = System.getProperty("user.dir"); // Ottiene la directory corrente
String imagePath = userDir+ File.separator + "greendot.png"; // Percorso relativo
logger.info("Image path = "+imagePath);
Image image = Toolkit.getDefaultToolkit().getImage(imagePath);
//Image image = Toolkit.getDefaultToolkit().getImage("./greendot.png");
PopupMenu popup = createTrayMenu();
TrayIcon ti = new TrayIcon(image, "Java System Tray Demo", popup);
ti.setImageAutoSize(true);
return ti;
}
the logged path is correct, and it looks like this
2024-06-19T10:23:12.416+02:00 INFO 5076 --- [ main] i.a.systemtrayDemo.sysTray.SysTrayIcon : Image path = C:UsersUSER1DesktopRandomDirNEWSOFTWAREgreendot.png