1

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();
        }
    }
} 
Ph33ly
  • 663
  • 3
  • 13
  • 29
  • what does `TrayIcon.class.getResource(path);` print? – Alex S. Diaz Aug 03 '15 at 15:10
  • i see you are experimenting a path nightmare... it can happen from time to time. Be sure to first define where the resource is placed... (1) if it is in the file system of the OS that hosts the JVM on where your app runs, OR (2) is bundled with the JAR that includes your App and can be can be retrieved using the class loader facilities (thus your resource must be accessible via classpath reference). First make up your mind where the resource is gonna be and how are you willing to access to it. – Victor Aug 03 '15 at 15:42
  • Perhaps this reading can help you out figuring some concepts... http://stackoverflow.com/questions/27845223/whats-the-difference-between-a-resource-uri-url-path-and-file-in-java – Victor Aug 03 '15 at 16:14

0 Answers0