Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What reasons have people had to write their own classloader [closed]

Tags:

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?

like image 215
Brett Hannah Avatar asked Feb 25 '09 13:02

Brett Hannah


People also ask

Why do we need custom ClassLoader in Java?

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.

When would you use a ClassLoader?

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.

Can we create our own ClassLoader in Java?

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.


1 Answers

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.

like image 144
Michael Borgwardt Avatar answered Nov 14 '22 19:11

Michael Borgwardt