Search code examples
javagetresource

Cannot access resource by using the getResources() method


I have the following project-tree:

enter image description here

I can access the 290.gif file in the path: Java GUI/src/dk/resources/290.gif by using the following command:

this.getClass().getResource("/dk/resources/290.gif")

But I can't access the 290.gif file in the path: Java GUI/resources2/290.gif by using the following command:

this.getClass().getResource("/resources2/290.gif")

I am trying to access the 290.gif file from the HelloWorldFrame.java class. I want to access the 290.gif file in the path: Java GUI/resources2/290.gif!

What am I doing wrong?


Solution

  • You have declared both src and resources2 directories as source folders in Eclipse. When Eclipse builds your app, it copies recources to the build folder, and so:

    • the files from dk.resources package are accessible via /dk/resources/290.gif
    • the files from resources2 folder are not in any package, and are accessible via /290.gif

    If you want the file to be accessible via /resources2/290.gif, then create a package named resource2 under the src folder.