Dushyant-GitHub commented on issue #3504:
URL: 
https://github.com/apache/logging-log4j2/issues/3504#issuecomment-2694833637

   > Can you clarify more what do you mean by "server classloader"? Are you 
talking about the parent of all web-app classloaders (see [Tomcat 
classloading](https://tomcat.apache.org/tomcat-11.0-doc/class-loader-howto.html)?
   
   When I mentioned "server classloader," I was referring to the classloader 
obtained via:
   `Thread.currentThread().getContextClassLoader()`
   This is the classloader that our application is using when dynamically 
loading extensions at runtime.
   
   > How do you dynamically add JARs to a running classloader? Classloader are 
usually designed to be immutable.
   
   We use a custom URLClassLoader reflection-based approach to dynamically load 
JARs at runtime. Below is the code we are using:
   ```
    URLClassLoader webappClassLoader = (URLClassLoader) 
Thread.currentThread().getContextClassLoader();
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", 
parameters);                                   
    method.setAccessible(true);                                                 
                                     
    method.invoke(webappClassLoader, new Object[] { jarUrl }); 
   ```
   With this approach, we are successfully loading JARs at runtime **without 
any immediate errors or exceptions**. We have verified through debugging that 
the **log4j-api JAR is also loaded properly** into the classloader.
   
   However, despite being loaded, we still encounter `NoClassDefFoundError` for 
`org.apache.logging.log4j.spi.ExtendedLogger`.
   
   The same application worked `without issues on Tomcat 9`. However, after 
upgrading to `Tomcat 11`, we observed this `NoClassDefFoundError`.
   
   > Do you have a root cause for the NoClassDefFoundError?
   
   The issue disappears after restarting the Tomcat 11 server. Once the server 
is restarted, everything works fine, and Log4j classes load without any errors.
   
   > If  `o.a.l.l.spi.ExtendedLogger` were to dynamically appear in a 
classloader, but the classloader already attempted to load the class, a 
NoClassDefFoundError will be thrown.
   
   Before loading the extension (which contains log4j-api), we explicitly 
checked whether the class `org.apache.logging.log4j.spi.ExtendedLogger` was 
already present in the classloader using:
   ```
   try {
       Class.forName("org.apache.logging.log4j.spi.ExtendedLogger");
       System.out.println("ExtendedLogger is already present.");
   } catch (ClassNotFoundException e) {
       System.out.println("ExtendedLogger is NOT present before extension 
import.");
   }
   ```
   This check confirmed that **the class was NOT present before loading the 
extension**.
   Even after successfully adding `log4j-api` to the classloader dynamically, 
we still get `NoClassDefFoundError` when trying to use Log4j, which is 
unexpected.
   
   We would appreciate any insights into why this behavior might occur in 
Tomcat 11 and whether Log4j's classloading mechanism has specific dependencies 
or behaviors that could be affected by dynamic JAR loading.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to