Search code examples
javafile-iogetresource

getResources() stops working when moved into package


The test method below passes when my test class is in the default package and fails when I move it into a package.

The directory structure is the following:

src/
  main/
    java/
      some.package/
    resources/
  test/
    FooTest.java // works here
    some.package/
        FooTest.java // does not work here

public class FooTest {

@Test
public void TestLoadImageFromFile() {
    BufferedImage loadedImage = null;
    try {
        loadedImage = ImageIO.read(this.getClass().getResource("someImage.png"));
    } catch(IOException e) {
        e.printStackTrace();
    }
    assertNotNull(loadedImage);
}
...

Solution

  • As is, your path is relative to the calling class. Use /someImage.png instead.