I'm trying to access classes from the servlet-api within a javaagent jar that is added to my application via the -javaagent:my-agent.jar flag. My application runs on Tomcat. The problem is that I get a ClassNotFoundException, because the agent jar is loaded by a classloader that has no access to the servlet-api classes.
More specifically, I want to include a ServletContainerInitializer in my javaagent
public class MyInitializer implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
System.out.println(ctx);
}
}
META-INF/services/javax.servlet.ServletContainerInitializer
org.example.MyInitializer
The result is a ClassNotFoundException because ServletContainerInitializer could not be found.
Is there any way to access the servlet-api within a javaagent? Or more generally, is it possible to access any class that is loaded via the ApplicationClassLoader, like classes from the spring framework?