ImageIO in Java

I'm a second year student, just starting to pick up Java. I have this bit of code and it keeps throwing an exception:

try
{
    masterFish = ImageIO.read(getClass().getResource("fish1.gif"));
} 
catch (IOException iOException)
{
    iOException.printStackTrace();
}

Can someone explain the part fish1.gif? Should I have an image called fish1.gifuploaded somewhere?

+4
source share
2 answers

See this question: Loading resources using getClass (). getResource ()

getResource() getClass() , ( .class) . , , . , com.example.MyClass, "fish1.gif" < > /src/com/example.

, , "" , .

+1

, "fish1.gif" . , "fish1.gif" , " ". "fish1.gif" , ... : "C:/Users/ name/Desktop/fish1.gif".

0

Source: https://habr.com/ru/post/1611217/


All Articles