Why am I getting a resource not found ?
I have a class that has the code:
Code:
final TrayIcon trayIcon =
new TrayIcon(createImage("c://aa.jpg", "tray icon"));
I definetly have a file called aa.jpg in c:\ ( I am using Windows )
When I try running my code I get:
Resource not found: c://aa.jpg
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: creating TrayIcon with null Image
at java.awt.TrayIcon.<init>(Unknown Source)
at TrayIconDemo.createAndShowGUI(TrayIconDemo.java:76 )
at TrayIconDemo.access$0(TrayIconDemo.java:68)
at TrayIconDemo$1.run(TrayIconDemo.java:63)
I have also tried with c:\\aa.jpg , did not help.
Thank you.
Re: Why am I getting a resource not found ?
Where is aa.jpg located? What does your createImage(...) method look like?
Re: Why am I getting a resource not found ?
aa.jpg is located at c:\aa.jpg
Code:
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();
}
}
Re: Why am I getting a resource not found ?
It appears that you're getting the image as a resource when you should be getting it as a file.
Re: Why am I getting a resource not found ?
Thanks, I will just skip this for now.. A bit advanced for me.
Re: Why am I getting a resource not found ?
The getResouce() mehod uses the classpath to find resources. You are giving it an absolute file path, not a path that is relative to the classpath.
Try moving the image to a folder on the classpath and changing the the path variable to use the relative path.
Re: Why am I getting a resource not found ?
Re: Why am I getting a resource not found ?
You can also specify a file resource; the URL would look like this: "file:///aa.jpg".
kind regards,
Jos
Re: Why am I getting a resource not found ?