getClassLoader().getResource() returns null

You don’t need the slash at the start when getting a resource from a ClassLoader, because there’s no idea of a “relative” part to start with. You only need it when you’re getting a resource from a Class where relative paths go from the class’s package level.

In addition, you don’t want Test.class.getClass() as that gets the class of Test.class, which will be Class<Class>.

In other words, try either of these lines:

URL viaClass=Test.class.getResource("/assets/pacman.png");
URL viaLoader=Test.class.getClassLoader().getResource("assets/pacman.png");

Leave a Comment