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?
我正在尝试使用自定义类装入器来练习,我有一些问题。有没有一种方法可以指示JVM全局使用我的自定义类装入器?例如,我编写了在Tomcat 6下运行的小应用程序。servlet由容器管理,我应该在哪里设置类装入器?另外,webapp使用了一些第三方jar,我可以控制这些jar的类装载吗?
Are the answers to the above will be different in case of standalone app?
如果是单独的app,上面的答案会不会有所不同?
Thanks!
谢谢!
1 个解决方案
#1
13
You can set the system default class loader as a JVM argument:
您可以将系统默认类装入器设置为JVM参数:
java -Djava.system.class.loader
=com.test.YourCustomClassLoader com.test.YourMainClass
As Tomcat starts as a java application, you can set this parameter too, at the %TOMCAT_HOME%\bin\run.bat
or $TOMCAT_HOME/bin/run.sh
executable.
当Tomcat作为java应用程序启动时,您也可以在%TOMCAT_HOME%\bin\run设置这个参数。蝙蝠或$ TOMCAT_HOME / bin /运行。sh可执行文件。
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 Loader
element for your context. The Loader element must be defined inside your Context
element, wich can be defined at a context.xml
file. More information:
编辑完成:如果您将类加载器设置为de System类加载器,它将用于加载Tomcat组件类、不同的库和您自己的类。如果希望类装入器仅用于应用程序类(包括库等),则应该为上下文配置装入器元素。加载器元素必须在上下文元素中定义,而wich可以在上下文中定义。xml文件。更多信息:
- Apache Tomcat: Class Loader HOW-TO: defines how the ClassLoaders work in Tomcat.
- Apache Tomcat:类装入器如何操作:定义类装入器在Tomcat中的工作方式。
-
Tomcat Configuration Reference: The Context Container: how to define your
Context
element - Tomcat配置引用:上下文容器:如何定义上下文元素。
-
Tomcat Configuration Reference: The Loader Component: How to define your custom
Loader
element for your ownContext
. - Tomcat配置引用:加载器组件:如何为自己的上下文定义自定义加载器元素。
#1
13
You can set the system default class loader as a JVM argument:
您可以将系统默认类装入器设置为JVM参数:
java -Djava.system.class.loader
=com.test.YourCustomClassLoader com.test.YourMainClass
As Tomcat starts as a java application, you can set this parameter too, at the %TOMCAT_HOME%\bin\run.bat
or $TOMCAT_HOME/bin/run.sh
executable.
当Tomcat作为java应用程序启动时,您也可以在%TOMCAT_HOME%\bin\run设置这个参数。蝙蝠或$ TOMCAT_HOME / bin /运行。sh可执行文件。
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 Loader
element for your context. The Loader element must be defined inside your Context
element, wich can be defined at a context.xml
file. More information:
编辑完成:如果您将类加载器设置为de System类加载器,它将用于加载Tomcat组件类、不同的库和您自己的类。如果希望类装入器仅用于应用程序类(包括库等),则应该为上下文配置装入器元素。加载器元素必须在上下文元素中定义,而wich可以在上下文中定义。xml文件。更多信息:
- Apache Tomcat: Class Loader HOW-TO: defines how the ClassLoaders work in Tomcat.
- Apache Tomcat:类装入器如何操作:定义类装入器在Tomcat中的工作方式。
-
Tomcat Configuration Reference: The Context Container: how to define your
Context
element - Tomcat配置引用:上下文容器:如何定义上下文元素。
-
Tomcat Configuration Reference: The Loader Component: How to define your custom
Loader
element for your ownContext
. - Tomcat配置引用:加载器组件:如何为自己的上下文定义自定义加载器元素。