How to Solve The classpathresource reads the resource file Error

If the file to be read is in jar, an error will be reported: java.lang.illegalargumentexception: URI is not hierarchical

Reason: the files in the jar package cannot be read in this way because the directory is opaque
original code

final ClassPathResource classPathResource = new ClassPathResource("logo.png");
final File file = classPathResource.getFile();
Image srcImg = ImageIO.read(file);

Solution:

final ClassPathResource classPathResource = new ClassPathResource("img/logo.png");
InputStream stream = classPathResource.getStream();
Image srcImg = ImageIO.read(stream);

Read More: