I’m trying to practice myself with custom class loaders, and I’ve some questions. Is there a way to indicate the JVM to use my custom class loader globally? For example, I wrote small app running under Tomcat 6. The servlet is managed by the container, where should I set my class loader? In addition, the webapp uses some 3rd party jars, can I control the classes loading of those jars?
Are the answers to the above will be different in case of standalone app?
Thanks!
You can set the system default class loader as a JVM argument:
As Tomcat starts as a java application, you can set this parameter too, at the
%TOMCAT_HOME%\bin\run.bator$TOMCAT_HOME/bin/run.shexecutable.Edit for completion:
If you set your classloader as de System class loader, it will be used to load Tomcat component classes, the different libraries, and your own classes.
If you want your class loader to be used only for your application classes (including libraries and so), you should configure a
Loaderelement for your context.The Loader element must be defined inside your
Contextelement, wich can be defined at acontext.xmlfile.More information:
ContextelementLoaderelement for your ownContext.