diff --git a/api/src/main/java/jakarta/mail/util/FactoryFinder.java b/api/src/main/java/jakarta/mail/util/FactoryFinder.java index e1f91b3e..9fae86f8 100644 --- a/api/src/main/java/jakarta/mail/util/FactoryFinder.java +++ b/api/src/main/java/jakarta/mail/util/FactoryFinder.java @@ -66,16 +66,23 @@ private static T newInstance(String className) throws RuntimeException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); checkPackageAccess(className); Class clazz = null; + T t = null; try { - if (classLoader == null) { - clazz = (Class) Class.forName(className); - } else { + if (classLoader != null) { clazz = (Class) classLoader.loadClass(className); } - return clazz.getConstructor().newInstance(); + t = clazz.getConstructor().newInstance(); } catch (ReflectiveOperationException e) { - throw new IllegalArgumentException("Cannot instance " + className, e); + //Going for second option + } + if(t == null) { + try { + t = (T)Class.forName(className).getConstructor().newInstance(); + } catch ( ReflectiveOperationException e) { + throw new IllegalArgumentException("Cannot instance " + className, e); + } } + return t; } private static String fromSystemProperty(String factoryId) {