7

Can Thread.getContextClassLoader() be null ? The javadoc is not really clear.
Should a library take this case into account ?

Update: the reason I asked is that beansbinding.dev.java.net does not work in this case (and my code does setContextClassLoader(null)

Junuxx
  • 14,011
  • 5
  • 41
  • 71
Tom
  • 55,743
  • 3
  • 27
  • 35

2 Answers2

12

Java threads created from JNI code in a non-java thread have null ContextClassloader unless the creator explicitly sets it.

Also in such context Thread.currentThread() returns null.

ddimitrov
  • 3,293
  • 3
  • 31
  • 46
  • 4
    This can be fixed by setting `ClassLoader cl = ClassLoader.getSystemClassLoader(); Thread.currentThread().setContextClassLoader(cl);` – Ujjwal Wadhawan Jan 21 '15 at 17:10
5

Yes you should, someone could have called Thread.setContextClassLoader(null) and your library IMO should be able to deal with that.

WMR
  • 12,661
  • 5
  • 35
  • 30