Jar hell: how to use a classloader to replace one jar library version with another at runtime

I can’t believe that for more than 4 years no one has answered this question correctly. https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When requested to find a class or resource, a ClassLoader instance will delegate the search … Read more

What loads the Java system classloader?

The Bootstrap classloader is the parent of all classloaders and loads the standard JDK classes in lib directory of JRE (rt.jar and i18n.jar). All the java.* classes are loaded by this classloader. The Extensions Classloader is the immediate child of the Bootstrap classloader. This classloader loads the classes in lib\ext directory of the JRE. The … Read more

cast across classloader?

Not possible. Class identity consists of the fully qualified name and the class loader. Casting an object to a class with the same name loaded by different classloaders is no different than trying to cast a String to Integer, because those classes really could be completely different despite having the same name.

How does class loading work when the same class exists in different applications on the same server?

A Java classloader typically works by looking for classes in one or more places in a fixed sequence. For instance, the classloader that loads your application when you run it from the command line looks first in the rt.jar file (and others on the bootclasspath), and then in the directories and JAR files specified by … Read more

Java security: Sandboxing plugins loaded via URLClassLoader

From the docs: The AccessControlContext of the thread that created the instance of URLClassLoader will be used when subsequently loading classes and resources. The classes that are loaded are by default granted permission only to access the URLs specified when the URLClassLoader was created. The URLClassLoader is doing exactly as its says, the AccessControlContext is … Read more

How are object dependencies between static blocks resolved?

The mechanism is described in detail here, but the five most important points are: Before a class is referenced, it needs to be initialised. If initialisation of a class has already begun (or if it’s finished), it isn’t attempted again. Before a class is initialised, all its superclasses and superinterfaces need to be initialised first. … Read more

CompletableFuture / ForkJoinPool Set Class Loader

I ran into something similar and came up with a solution that does not use reflection and seems to work well with JDK9-JDK11. Here is what the javadocs say: The parameters used to construct the common pool may be controlled by setting the following system properties: java.util.concurrent.ForkJoinPool.common.threadFactory – the class name of a ForkJoinPool.ForkJoinWorkerThreadFactory. The … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)