1

So I'm experimenting with the tray menu and I have the line.

final TrayIcon trayIcon = new TrayIcon(createImage("duke.jpg", "tray icon"));

The method createImage is

protected static Image createImage(String path, String description) {
    URL imageURL = TrayIconDemo.class.getResource(path);

    if (imageURL == null) {
        System.err.println("Resource not found: " + path);
        return null;
    } else {
        return (new ImageIcon(imageURL, description)).getImage();
    }
}

when I run the program I get the following error

Resource not found: duke.jpg
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: creating TrayIcon with null Image
    at java.awt.TrayIcon.<init>(Unknown Source)
    at misc.TrayIconDemo.createAndShowGUI(TrayIconDemo.java:76)
    at misc.TrayIconDemo.access$0(TrayIconDemo.java:68)
    at misc.TrayIconDemo$1.run(TrayIconDemo.java:63)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Here is a picture of my folder showing where the image is.

https://i.stack.imgur.com/80tFY.png

I would just like to state that I found this http://www.oracle.com/technetwork/articles/javase/systemtray-139788.html and it works regardless if there is an image or if there isn't. Also the image had to be placed in the source folder and NOT the folder the .java was in for it to show so I'm assuming it has to be there for the original code that my questions is in regards to. So if anyone needs help with the System Tray just check the link.

Maple
  • 17
  • 6
  • Even placing the picture as the same folder as the .java I still get the error. – Maple Jun 25 '13 at 19:42
  • @SotiriosDelimanolis Here it is. http://gyazo.com/179ba9c1e892b8e2d8cab3b7e089ca51.png – Maple Jun 25 '13 at 19:49
  • @SotiriosDelimanolis Placing the image there also provides me with the same error. – Maple Jun 25 '13 at 19:52
  • Cleaned it and restarted eclipse and rebuilt the project. Still the same problem. Would you like to see it via TeamViewer? – Maple Jun 25 '13 at 20:01
  • I did that with the image in both the Screen folder (Project Folder) and in the same folder as the .java of the program I'm running. Then I tried it with the image in only of of them and each didn't work. Also it's .jpg. http://gyazo.com/53d60b5fc089ee9b595e9244c67630ad.png – Maple Jun 25 '13 at 20:06
  • It has to be in the same folder as the java file. – Sotirios Delimanolis Jun 25 '13 at 20:07
  • I did try it in that folder. Then cleaned and rebuild the project. – Maple Jun 25 '13 at 20:08
  • http://gyazo.com/e8e95a4fc90a76305cae58617ebb75d8.png Yup it's .jpg. Any other solutions? Still not working. – Maple Jun 25 '13 at 20:12
  • I don´t know it exactly, but I *think* you can´t use TrayIconDemo.class.getResource(...) - try removing "static" from the function and use this.getClass().getClassloader().getResource(...) instead – cljk Jun 25 '13 at 20:13
  • @cljk What makes you think that? – Sotirios Delimanolis Jun 25 '13 at 20:13
  • I had this case on an app-server... not using "getResource" but "getResourceAsStream" and I can´t really remember what exactly was the cause... I would just try it out. – cljk Jun 25 '13 at 20:15
  • @cljk `getResourceAsStream()` internally calls `getResource()` and then calls `openStream()` on the `URL` if it isn't `null`. – Sotirios Delimanolis Jun 25 '13 at 20:16
  • @Maple Here's the javadoc for `getResource()`: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String) – Sotirios Delimanolis Jun 25 '13 at 20:20
  • Not sure if this helps but the code is from http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/TrayIconDemoProject/src/misc/TrayIconDemo.java Every thing is the same except that line with the picture. – Maple Jun 25 '13 at 20:21
  • @Maple In that example, the image file would be in a folder/package in the package that the `TrayIconDemo` class file is in. Given `images/bulb.gif`, it would have to be in (example) `src/misc/images/bulb.gif`. For your example, the `duke.jpg` should be at `src/misc/duke.jpg`. The path you specify is relative to the package your class is in. – Sotirios Delimanolis Jun 25 '13 at 20:24
  • I did just that for both bulb and duke. Sadly both are providing me with the same error. – Maple Jun 25 '13 at 20:34
  • @Maple The last thing I could suggest is to try and create a new project and copying the files over. Your config is correct. – Sotirios Delimanolis Jun 25 '13 at 20:37
  • Did just that and still the same problem. Thanks for trying to help anyway. – Maple Jun 25 '13 at 20:41
  • @SotiriosDelimanolis Do you know any other way I can add a JFrame to the tray bar? – Maple Jun 25 '13 at 20:47
  • @Maple Sorry, I don't know much about swing. Another thing you can do is `Export` with eclipse and check where the `duke.jpg` file is in the generated `jar` (or if it isn't even there). – Sotirios Delimanolis Jun 25 '13 at 20:52
  • 1) What IDE do you use? I don't think that image is in the right place for an IDE to include it in the final Jar. 2) Do a `jar -tvf the.jar` and copy/paste the output as an [edit](http://stackoverflow.com/posts/17306024/edit) to the question. 3) Steer clear of using `static` in that method name, use an instance of `TrayIconDemo` and prefix the path with `/`. – Andrew Thompson Jun 26 '13 at 17:48

2 Answers2

0

The easiest way you can do is System.out.println(new File(".").getAbsolutePath()) to find out what is considered as a root path and then adjust the actual path to the file.

I use this because there is a difference between webapp (Servlet-based) and Java SE app.

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
0

If you change this method like this, it works.

protected static Image createImage() {
            String path="bulb.gif";
            String description="";
            String imageURL = path;
            if (imageURL == null) {
                System.err.println("Resource not found: " + path);
                return null;
            } else {
                return (new ImageIcon(imageURL, description)).getImage();
            }
        }
Unnamed
  • 70
  • 1
  • 2
  • 9