I was recently asked in an interview about the order in which classloaders are called when a class is loaded.
Unfortunately I've never had the need to write my own classloader so at the time was unfamiliar with the intricacies of classloading.
This got me wondering, what reasons are their to write your own classloader.
So that's my question: What scenarios have people faced which required the need to writing their own classloaders?
With a custom classloader you can add behaviour to the loaded classes before they are passed over to the running application. Show activity on this post. Java class loaders do pretty much what the name suggests: load classes into memory so that they can be used.
Java uses ClassLoader implicitly when you use new , import keyword, the jvm will use the current class's classloader to load the dependent classes, so you can use the custom classloader to load a bootstrap class explicitly by using classloader.
We will create our own ClassLoader by extending the ClassLoader class and overriding the loadClass(String name) method. If the class name will start from com. journaldev then we will load it using our custom class loader or else we will invoke the parent ClassLoader loadClass() method to load the class.
I'm currently working on an extremely large application that is highly modularized, i.e. it consists of literally hundreds of JAR files. This meant that the classpath string became huge, causing all sorts of problems in all sorts of places, because of various development tools's inability to deal with a 5KB classpath string. This was solved by writing a custom classloader that reads its classpath from a file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With