How to explore which classes are loaded from which JARs?
Passing the -verbose:class switch to the java command will print each class loaded and where it was loaded from. Joops is also a nice tool for finding missing classes ahead of time.
Passing the -verbose:class switch to the java command will print each class loaded and where it was loaded from. Joops is also a nice tool for finding missing classes ahead of time.
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
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
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.
One of the best ways would be using the enum polymorphism technique: public class EnumTest { public enum Foo { A { @Override public Bar getBar() { return Bar.Alpha; } }, B { @Override public Bar getBar() { return Bar.Delta; } }, C { @Override public Bar getBar() { return Bar.Alpha; } }, ; public … Read more
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
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
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
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