Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Custom Class Loader

Recently I came accross the java custom class loader api. I found one use over here, kamranzafar's blog I am a bit new to the class loader concept. Can any one explain in detail, what are the different scenarios where we may need it or we should use it?

like image 946
Priyank Doshi Avatar asked May 31 '12 07:05

Priyank Doshi


People also ask

What is the use of custom ClassLoader in Java?

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.

What is the use of class loader?

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They're also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

How do I create a custom class loader?

To create new custom class loaders, the process is similar in the Java SE 7 release as in previous releases. Create a subclass of ClassLoader , then override the findClass() method and possibly loadClass() .

Can we create custom 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.


2 Answers

Custom class loaders are useful in larger architectures consisting of several module/applications. Here are the advantages of the custom class loader:

  • Provides Modular architecture Allows to define multiple class loader allowing modular architecture.
  • Avoiding conflicts Clearly defines the scope of the class to within the class loader.
  • Support Versioning Supports different versions of class within same VM for different modules.
  • Better Memory Management Unused modules can be removed which unloads the classes used by that module, which cleans up memory.
  • Load classes from anywhere Classes can be loaded from anywhere, for ex, Database, Networks, or even define it on the fly.
  • Add resources or classes dynamically All the above features allows you add classes or resources dynamically.
  • Runtime Reloading Modified Classes Allows you to reload a class or classes runtime by creating a child class loader to the actual class loader, which contains the modified classes.
like image 126
Ramesh PVK Avatar answered Oct 11 '22 03:10

Ramesh PVK


The primary use is in Application servers so that they can run two applications and not have the classes conflict. i.e. if application 1 has a class with the same name as application 2, with a custom class loader application 1 will load its class and application 2 will load its class.

Also if a class is loaded by a custom class loader it is possible to unload that class from the JVM. Again useful in application servers.

Another use would be for instrumentation - One way of doing aspect oriented programming or when using some persistence API's. With a custom classloader you can add behaviour to the loaded classes before they are passed over to the running application.

like image 44
Michael Wiles Avatar answered Oct 11 '22 01:10

Michael Wiles