Can a thread context class loader be null?

I'm not quite sure how I got into this situation, but somehow I get a null ClassLoader from Thread.getContextClassLoader. After a little reading (there is not much information in the documents or in google) I got the impression that there really is a null class loader for the current thread, and the calls to getContextClassLoader should be checked for a null reference.

This is quite surprising since I saw a couple of open source projects that were not tested on getContextClassLoader (which allowed me to check this first). Specifically, this line in codemodel : line JCodeModel.java 358

(I just checked that log4j doesn't check either)

So, do I need to call getContextClassLoader to check for a null reference, or is something messed up with my threads?

+3
source share
2 answers

Valid for Thread.getContextClassLoaderreturn null. Not all software is particularly good quality.

While it null ClassLoaderusually refers to a boot class loader that loads system classes (I think that's right - the terminology is confused for historical reasons), for context context class loaders, it is usually interpreted as uninstalled, and the system class loader is used instead.

IIRC, , java. EDT .

( ), .

+3

, Thread.getContextClassLoader() null. , ( JVM- - ).

, ClassLoader , , /.

Class.getClassLoader() null .

0

Source: https://habr.com/ru/post/1759146/


All Articles