From 65001875b58726bccce9913c2e9639c6ff39512b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 2 Dec 2025 15:58:13 -0800 Subject: [PATCH] Use the proper servlet classloader here. Otherwise if samskivert.jar is loaded via the root classloader, this will fail to find classes and resources in the servlet/webapp classloader. Let's not require that samskivert.jar be duplicated into the webapp in cases where we're doing complex things like hosting webapps inside larger app servers. That can cause other fiddly problems with mismatched duplicate copies of classes. --- .../java/com/samskivert/velocity/DispatcherServlet.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/samskivert/velocity/DispatcherServlet.java b/src/main/java/com/samskivert/velocity/DispatcherServlet.java index 12fb65ec..a65a41fc 100644 --- a/src/main/java/com/samskivert/velocity/DispatcherServlet.java +++ b/src/main/java/com/samskivert/velocity/DispatcherServlet.java @@ -185,7 +185,8 @@ public class DispatcherServlet extends HttpServlet if (StringUtil.isBlank(appcl)) { return new Application(); } else { - Class appclass = Class.forName(appcl); + ClassLoader servletClassLoader = Thread.currentThread().getContextClassLoader(); + Class appclass = servletClassLoader.loadClass(appcl); return (Application)appclass.getDeclaredConstructor().newInstance(); } } @@ -202,7 +203,8 @@ public class DispatcherServlet extends HttpServlet "in the servlet configuration."); } // config util loads properties files from the classpath - Properties props = ConfigUtil.loadProperties(propsPath); + ClassLoader servletClassLoader = Thread.currentThread().getContextClassLoader(); + Properties props = ConfigUtil.loadProperties(propsPath, servletClassLoader); if (props == null) { throw new IOException("Unable to load velocity properties " + "from file '" + INIT_PROPS_KEY + "'."); @@ -596,7 +598,8 @@ public class DispatcherServlet extends HttpServlet */ protected Logic instantiateLogic (String path, String lclass) { try { - Class pcl = Class.forName(lclass); + ClassLoader servletClassLoader = Thread.currentThread().getContextClassLoader(); + Class pcl = servletClassLoader.loadClass(lclass); return (Logic)pcl.getDeclaredConstructor().newInstance(); } catch (ClassNotFoundException cnfe) { // nothing interesting to report