Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By JosAH

Thread: Why am I getting a resource not found ?

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Why am I getting a resource not found ?

    I have a class that has the code:
    Java 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why am I getting a resource not found ?

    Where is aa.jpg located? What does your createImage(...) method look like?

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Why am I getting a resource not found ?

    aa.jpg is located at c:\aa.jpg

    Java 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();
            }
        }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Why am I getting a resource not found ?

    Thanks, I will just skip this for now.. A bit advanced for me.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.
    Fubarable likes this.

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Why am I getting a resource not found ?

    Thanks Norm.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default 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
    Norm likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Why am I getting a resource not found ?

    Thanks, I will try both.

Similar Threads

  1. closing the use of a resource
    By jayragz in forum New To Java
    Replies: 14
    Last Post: 05-17-2011, 05:24 PM
  2. JAVA resource(s)
    By Sparky in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 02:26 AM
  3. resource not found error- Struts 2 (Struts2 in action- Manning)
    By JavaNoobie87 in forum Web Frameworks
    Replies: 0
    Last Post: 01-17-2011, 11:09 AM
  4. Resource File
    By dvlchd3 in forum Eclipse
    Replies: 2
    Last Post: 01-13-2010, 11:32 AM
  5. The requested resource is not available.
    By ravichandra.t in forum Java Servlet
    Replies: 0
    Last Post: 07-29-2009, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •