Search code examples
javaswingembedded-resource

How to verify a img file existed before setIcon it to JLabel in case clean and build to JAR file?


I am designing a Java program to set icon to JLabel when clicking the corresponding button. I want to know ways to check whether the img exists before calling:

ImageIcon myIcon = new ImageIcon(getClass().getClassLoader().getResource(path));

I'm not allowed to use try catch!


Solution

  • If the image does not exist in the supplied path, the URL returned from getResource(..) will be null.