The situation at hand is not as simple as the title seems to indicate.
Java 1.6_17 running via JWS.
I have a class, lets say MyClass
and one of its instance member variables is a Type from an errant 3rd party library where during class initialization it dynamically tries loading some of its own classes with Class.forName(String)
. In one of these cases it happens to dynamically call: Class.forName("foo/Bar")
.This class name doesn't follow the JLS for binary names and ultimately leads to a java.lang.NoClassDefFoundError: foo/Bar
.
We have a custom ClassLoader
which I've added a sanitize method to ClassLoader.findClass(String)
and ClassLoader.loadClass(String)
which fixes this problem.
I can call stuff like:
myCustomClassLoader.findClass("foo/Bar")
Which then loads the class without any problems. But even if I load the class ahead of time, I still get the exception later. This is because during initialization of MyClass
which refers to Bar
- their code ends up calling Class.forName("foo/Bar")
in a static block somewhere. This actually would be OK if the ClassLoader it was trying to use was my custom class loader. But it isn't. It is the com.sun.jnlp.JNLPClassLoader
which doesn't do such sanitation, thus my problem.
I've made sure that Thread.currentThread().getContextClassLoader()
is set to my custom class loader. But this (as you know) has no effect. I even set it as the first thing i do in main()
due to some stuff I read and still, MyClass.class.getClassLoader()
- is the JNLPClassLoader. If I could force it to NOT be the JNLPClassLoader and to use mine instead, problem solved.
How can I control which ClassLoader is used to load the class via their static Class.forName("foo/Bar") call made during class initialization? I believe if I can force MyClass.class.getClassLoader()
to return my custom class loader, my problem will be resolved.
I'm open to other options if anyone has ideas.
TL;DR: Help me force all Class.forName(String)
calls in a third party library which are referenced by MyClass
- to use the classloader of my choosing.