Is there a quick way to grab the contents of an image file from a signed.jar? I have a Java file that creates a TrayIcon in the SystemTray which requires an image. My Java class works fine when tested, but breaks when I use it in my software and it seems that my URL object is the problem. Previously I was using TrayIconD.class.getResource(path);
but it doesn't look like that works with my signed.jar file as they have different file names. Is there a way I can still use the above method or use a URL object with getContent()
instead? Nether of these methods work for me
import java.awt.TrayIcon;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.AWTException;
import javax.swing.ImageIcon;
import java.net.URL;
public class TrayIconD {
public static void main(String[] args) {
createAndShowGUI();
}
private static void createAndShowGUI() {
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
final SystemTray tray = SystemTray.getSystemTray();
final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
return;
}
trayIcon.displayMessage("Sun TrayIcon Demo", "This is an info message", TrayIcon.MessageType.INFO);
}
protected static Image createImage(String path, String description) {
//URL imageURL = TrayIconD.class.getResource(path);
URL imageURL = null;
Object image = null;
try{
imageURL = new URL("/C:/Users/Documents/notifications/images/bulb.gif");
// System.out.println(imageURL);
// image = imageURL.getContent();
}
catch(Exception ep){
System.out.println(ep.getMessage());
}
if (imageURL == null) {
System.err.println("1111Resource not found: " + path);
return null;
} else {
return (new ImageIcon(imageURL, description)).getImage();
}
}
}