What is the purpose of a Java class loader?

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

I would be glad if you could give an example of the situation when you use the class loader and why you use the class loader in this situation.

+6
source share
7 answers

when the JVM loads all the classes in the project.

The JVM loads classes more or less "on demand". That is, all classes at runtime will usually not load after startup.

See these URLs for more details:

Why do we need the concept of a class loader

Class loaders allow us to load classes from different sources.

  • jar file on disk
  • generated byte array of runtime
  • from the Internet (which is typical for applets)

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

give me an example with the situation when you use the class loader and why you use the class loader.

Without a loader class you will not go far, so I will interpret your question as "when you need a custom class loader."

Personally, I did some experiments using the Byte Manipulation Library ( ASM ), where I replaced the field calls with the get- and set-method calls. I used a special class loader to overwrite classes as they load. I do not know if this is a typical use case, but the fact is that I could not have done it without it!

You can also imagine a plug-in system that loads peripheral classes from some plug-in directory.

+13
source

A class is loaded whenever it is executed directly or if it refers to another class that must be executed ... for example

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

here whenever u gets an executable class B, 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 } } 
+2
source

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

 static { MyClass.class.getName(); } 

A custom class loader is rarely needed, in most cases: AOP (for example, runtime when loading classes from Javassist), loading a remote class (loading a class from a remote location), encrypted class loading (decrypting the class code and loading).

+1
source

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

+1
source

The JVM loads the class the first time it is accessed. For an in-depth analysis of Class Loaders, see here.

+1
source

In many cases, a class loader is used. A few examples:

  • Class.forName to run Java classes at runtime
  • Reflection API
  • Eclipse Debugger

There are also many other examples.

0
source

By default, the Loader class only loads the .class file once, even if you use it several times in your program. After the load.class file, if it is modified externally, then the class loader will not load the updated version of the class file by default (the .class file is already available in the method area). You can solve this problem by specifying your own custom classloader.

The main advantage of a custom class loader is the management of the class loading mechanism based on your requirement.

java.lang.ClassLoader to define your own custom classloader. Each class loader in JAVA must be a child of the java.lang.ClassLoader class, or directly indirectly. Therefore, this class acts as the base class for all custom classloaders.

Note. . When designing / developing web servers and application servers, typically custom class loaders are used for a custom class loading mechanism.

For instance:

 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"); // // } } 
0
source

Source: https://habr.com/ru/post/891627/


All Articles