Search code examples
javaembedded-resourceimageicon

getResource() -> Source not found


I'm following the tutorial here. The file is in the same root folder of the project. I've tried doing it when it's in the src folder and in the same package folder. None of those 3 locations worked.

The specific line of code is:

ImageIcon ii = new ImageIcon(this.getClass().getResource("bardejov.jpg"));

Any idea what I'm doing wrong?


Solution

  • // absolute from the classpath
    MyClass.class.getResource("/myfolder/abc.txt");
    // relative to the class location
    MyClass.class.getResource("abc.txt");
    // another relative to the class location
    MyClass.class.getResource("myfolder/abc.txt");