ClassLoading is a very complicated question. The Security Model ClassLoader and Java are inextricably linked. Essentially, the JVM loads classes on demand. When there is a hierarchy of class loaders, the JVM tries to resolve the class as far down the chain as possible. In short, if the class is defined in the bootloader and in the classloader defined by the application, it will always use the version in the bootloader.
Inside a class loader such as URLClassLoader, the search order is the order in which you told it to look. Essentially, the array of URLs you told him had classes will search from the first record to the last.
When the class that you defined refers to another class, that class is also resolved using the same algorithm. But here's the catch: he only decides it as to where he was found. Let's take a scenario where the SomeCoolThing class is in the bootloader of the classes, but depends on SomeLameThing, which is in the application-specific classloader. The process will look like this:
App-ClassLoader: resolveClass("SomeCoolThing") parent->resolveClass("SomeCoolThing") Boot-ClassLoader (the ultimate parent): resolveClass("SomeCoolThing") SomeCoolThing needs SomeLameThing resolveClass("SomeLameThing")
Although SomeLameThing is located in the classloader where you requested SomeCoolThing, SomeCoolThing was enabled in another classloader. This other classloader is unaware of the classloader and is trying to solve it on its own.
I had a long-standing book that covered Java ClassLoaders at a very good depth, and I recommend it. This is Java Security from O'Reilly Media . It will answer every question that you never wanted to know, but you still need to deal with ClassLoaders and how they work.
source share