I have the following project-tree:
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?
Seems that the folders
src
andresources2
are both in the build path. Have you triedthis.getClass().getResource("/290.gif")
?During runtime of the artifact you can access resources with
or
where SomeClass is a class in the artifact. The first way will give you an InputStream that you can use to read the contents of the file, the second way will return a URL that you can use to find out the path of the file or directory or move along the directory tree.
You have declared both
src
andresources2
directories as source folders in Eclipse. When Eclipse builds your app, it copies recources to thebuild
folder, and so:dk.resources
package are accessible via/dk/resources/290.gif
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 namedresource2
under thesrc
folder.