简体   繁体   中英

What is the purpose of the Java Class loader?

My question is, when does JVM load all the classes in the project? Also, why do we need the notion of a class loader.

I'd be happy if you could give me a example of a situation where you use class loader and why you use class loader in that situation.

when does JVM load all the classes in the project.

The JVM loads the classes more or less "on demand". Ie all classes in the runtime will typically not be loaded upon launch.

Refer to these URLs for details on this topic:

why do we need the notion of a class loader

Class loaders allow us to load classes from various sources.

  • a jar file on disk
  • a runtime generated byte-array
  • from the Internet (which is a typical use case for applets)

This makes the launch of an application more flexible and modular.

give me a example with situation where you use class loader and why you use class loader there.

Without a class-loader you won't get far, so I'll interpret your question as "when do you need a custom class loader".

Personally I did some experiments using a byte-code manipulation library ( ASM ) where I replaced field accesses with get- and set-method calls. I used a custom class loader to rewrite the classes as they were loaded. I don't know if it's a typical use case, but the point is that I couldn't have done this without one!

You could also imagine a plugin-system which loads peripheral classes from some plugin directory.

A class is loaded whenever it is executed directly orif it is referenced in another class which is to be executed... for example

class A
{}  
class B extends A  
{  
  public static void main(String arr[])  
  {}  
}  

here whenever u get execute class B,the class A is loaded automatically

now consider this

class A  
{}  

class B  
{  
  public static void main(String arr[])  
  {  
    A ob=new A();//here class A is need to be loaded by JRE  
  }  
}

JVM loads a Class the first time it is referenced. For in depth analysis of Class Loaders look here

JVM load classes on demand. When you need class to be explicitly loaded, you need to make reference to that class from the main class, for example


static {
    MyClass.class.getName();
}

Custom classloader is rarely needed, most commons cases are: AOP (for example runtime on-load instrumentation of classes with Javassist), remote class loading (loading a class from remote location), encrypted class loading (deciphering class code and loading).

You use class Loader for loading classes if you are developing application which can support plugins. Sample: You have application for video player and each codec is plugin in your application. you have folder./codecs and there you put your plugin codecs. You search the folder for jar files and load all jar files with Class loader.

Class loader is used in many cases. Few examples are:

  1. Class.forName to get Java classes at runtime
  2. Reflection API
  3. Eclipse debugger

There are lot of other examples as well.

Default Class Loader will load.class file only once, even though you are using that multiple times in your program. After loading.class file, if it is modified outside then default class loader wont load updated version of class file(.class file is already available in method area). You can resolve this problem, by defining your own customized Class Loader.

Main advantage of customized class loader is you can control, class loading mechanism based on your requirement.

java.lang.ClassLoader to define own customized class loader. Every class loader in JAVA should be child class of java.lang.ClassLoader class, either directly of indirectly. Hence, this class acts as base class for all customized class loaders.

Note: While designing/developing web servers and application server, usually customized class loaders are used to customized class loading mechanism.

For example:

public class CustClassLoader extends ClassLoader{
     public Class loadClass(String cname) throws ClassNotFoundException{
     //check for updates and laod updated .class
     //file and returns corresponding Class
     }
}

class Client{
    public static void main(String [] args){
         Dog d1 = new Dog();
         CustClassLoader c1 = new CustClassLoader();
         c1.loadClass("Dog");
         //
         // 
         //
         c1.loadClass("Dog");
         //
     //
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM