Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

java - PMD rule "Use Proper Class Loader" explaination?

There is the following PMD rule:

Use Proper Class Loader (Critical)

In J2EE getClassLoader() might not work as expected. Use Thread.currentThread().getContextClassLoader() instead.

Can somewhat explain more what they are thinking of? What exactly means "J2EE" environment here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...