I am following the zetcode Snake java games tutorial and always get this error:
ImageIcon iid = new ImageIcon(this.getClass().getResource(\"ball.png\"));
bal
The resource so named wasn't found. It needs to be in the same directory as the .class file you are calling it from. See the Javadoc.
You may need to add the file to your build resources, something like this:
<build>
<resources>
<resource>
<directory>path\to\resources</directory>
<includes>
<include>ball.png</include>
</includes>
</resource>
</resources>
You have to put the image file(ball.png) into your classpath. More details, please take a look at the Javadoc.
Go to project >clean in the eclipse it would refresh the package explorer and you won't face this problem anymore.
You can use only path of your image. I think this will help you: Use this:
ImageIcon iid = new ImageIcon("C:\\Users\\ranig\\My\\spaceinvaders\\ball.png");
Note: C:\\Users\\ranig\\My\\spaceinvaders\\ball.png
is the whole path of ball.png image.
instead of this:
ImageIcon iid = new ImageIcon(this.getClass().getResource("ball.png"));
Note: If u want to only try snake code and only want to get output.
The image should be in the same package (folder in OS terms) as the compiled class. Check whether you have both .class
and .png
in the same folder. If not, you can use classpath-relative paths in getResource(..)
, by starting with /