diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java index 1608792e7f..81cc71e671 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java @@ -73,7 +73,19 @@ public class LaunchedURLClassLoader extends URLClassLoader { throws ClassNotFoundException { Handler.setUseFastConnectionExceptions(true); try { - definePackageIfNecessary(name); + try { + definePackageIfNecessary(name); + } + catch (IllegalArgumentException ex) { + // Tolerate race condition due to being parallel capable + if (getPackage(name) == null) { + // This should never happen as the IllegalArgumentException indicates + // that the package has already been defined and, therefore, + // getPackage(name) should not return null. + throw new AssertionError("Package " + name + " has already been " + + "defined but it could not be found"); + } + } return super.loadClass(name, resolve); } finally { @@ -92,7 +104,20 @@ public class LaunchedURLClassLoader extends URLClassLoader { if (lastDot >= 0) { String packageName = className.substring(0, lastDot); if (getPackage(packageName) == null) { - definePackage(packageName); + try { + definePackage(packageName); + } + catch (IllegalArgumentException ex) { + // Tolerate race condition due to being parallel capable + if (getPackage(packageName) == null) { + // This should never happen as the IllegalArgumentException + // indicates that the package has already been defined and, + // therefore, getPackage(name) should not have returned null. + throw new AssertionError( + "Package " + packageName + " has already been defined " + + "but it could not be found"); + } + } } } }