I have a folder called Etc which has an image I want to use in another file in the same folder, Example.java. So I have Etc\image.png and Etc\Example.java. I’ve tried using “Etc/image.png” as the image path, but that didn’t work. How should I go about this?
Also, suppose the files were in different packages, how would I do it?
My main .java classes are in a package called Main, for the record.
EDIT:
I used this:
ClassLoader.getSystemClassLoader().getResource(“Etc\image.png”);
You can use
Class.getResource(), which uses the class loader to obtain a URL to the resource. For example:The important part is
Example.class.getResource( "image.png" )– the image path is specified relative to the named class; in this case, it’s in the same directory as the class file. You could also use this line in any other class, leaving the reference toExample.classintact.