diff --git a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java index c618dfddbd..6374f2768b 100644 --- a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -148,14 +148,21 @@ public class ClassPathResource extends AbstractFileResolvingResource { */ @Nullable protected URL resolveURL() { - if (this.clazz != null) { - return this.clazz.getResource(this.path); + try { + if (this.clazz != null) { + return this.clazz.getResource(this.path); + } + else if (this.classLoader != null) { + return this.classLoader.getResource(this.path); + } + else { + return ClassLoader.getSystemResource(this.path); + } } - else if (this.classLoader != null) { - return this.classLoader.getResource(this.path); - } - else { - return ClassLoader.getSystemResource(this.path); + catch (IllegalArgumentException ex) { + // Should not happen according to the JDK's contract: + // see https://github.com/openjdk/jdk/pull/2662 + return null; } }