PMD rule “Use Proper Class Loader” explaination?

后端 未结 1 629
一整个雨季
一整个雨季 2020-12-19 20:46

There is the following PMD rule:

Use Proper Class Loader (Critical)

In J2EE getClassLoader() might not work as expected. Use Th

相关标签:
1条回答
  • 2020-12-19 21:04

    J2EE environment refers to the Java Enterprise Edition. Here, you do not run your program as a standalone application on the JVM, but instead, you let the JEE Application Server (e.g. Glassfish or Tomcat) to run it for you.

    Application Servers provide a lot of different ways to run your program, for example they offer concurrent and distributed execution. AppServers play with classloaders, sometimes there is a hierarchy, for example the EAR (Enterprise ARchive) has one, and a WAR (Web ARchive) inside the EAR has a different one. Because of this it is not ensured you will always get the right ClassLoader with a getClassLoader() call. With Thread.currentThread().getContextClassLoader() you will always get the ClassLoader that loaded your app.

    (Note: This is not entirely true, some applications make trick with ContextClassLoader (like Spring, OSGi). Still, this is the best way to get your proper ClassLoader).

    0 讨论(0)
提交回复
热议问题