简体   繁体   English

Java Class 装载机的用途是什么?

[英]What is the purpose of the Java Class loader?

My question is, when does JVM load all the classes in the project?我的问题是,JVM 什么时候加载项目中的所有类? Also, why do we need the notion of a class loader.另外,为什么我们需要 class 加载程序的概念。

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.如果您能给我一个示例,说明您使用 class 加载程序以及为什么在这种情况下使用 class 加载程序,我会很高兴。

when does JVM load all the classes in the project. JVM 什么时候加载项目中的所有类。

The JVM loads the classes more or less "on demand". JVM 或多或少地“按需”加载类。 Ie all classes in the runtime will typically not be loaded upon launch.即,运行时中的所有类通常不会在启动时加载。

Refer to these URLs for details on this topic:有关此主题的详细信息,请参阅这些 URL:

why do we need the notion of a class loader为什么我们需要 class 加载程序的概念

Class loaders allow us to load classes from various sources. Class 加载器允许我们从各种来源加载类。

  • a jar file on disk磁盘上的 jar 文件
  • a runtime generated byte-array运行时生成的字节数组
  • from the Internet (which is a typical use case for applets)来自 Internet(这是小程序的典型用例)

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.给我一个例子,说明你使用 class 装载机的情况,以及为什么你在那里使用 class 装载机。

Without a class-loader you won't get far, so I'll interpret your question as "when do you need a custom class loader".如果没有类加载器,您将走不了多远,因此我将您的问题解释为“您何时需要自定义class 加载器”。

Personally I did some experiments using a byte-code manipulation library ( ASM ) where I replaced field accesses with get- and set-method calls.就我个人而言,我使用字节码操作库 ( ASM ) 进行了一些实验,其中我用 get 和 set 方法调用替换了字段访问。 I used a custom class loader to rewrite the classes as they were loaded.我使用自定义 class 加载器在加载类时重写它们。 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 或在另一个要执行的 class 中引用它时,都会加载 class ... 例如

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在这里,每当您执行 class B 时,自动加载 class A

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. JVM 在第一次被引用时加载 Class。 For in depth analysis of Class Loaders look here有关 Class 装载机的深入分析,请看这里

JVM load classes on demand. JVM 按需加载类。 When you need class to be explicitly loaded, you need to make reference to that class from the main class, for example当您需要显式加载 class 时,您需要从主 class 中引用该 class,例如


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). 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.如果您正在开发可以支持插件的应用程序,您可以使用 class 加载器来加载类。 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.您在文件夹中搜索 jar 文件并使用 Class 加载程序加载所有 jar 文件。

Class loader is used in many cases. Class loader 用在很多情况下。 Few examples are:几个例子是:

  1. Class.forName to get Java classes at runtime Class.forName 在运行时获取 Java 类
  2. Reflection API反射 API
  3. Eclipse debugger Eclipse 调试器

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.默认 Class 加载程序将仅加载一次.class 文件,即使您在程序中多次使用该文件。 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).加载.class 文件后,如果在外部修改,则默认 class 加载器不会加载 class 文件的更新版本(.ZA2F2ED4F2ED4F2EBC2CBB61DZC4 方法文件1 已在区域可用)。 You can resolve this problem, by defining your own customized Class Loader.您可以通过定义自己的自定义 Class 加载程序来解决此问题。

Main advantage of customized class loader is you can control, class loading mechanism based on your requirement.定制 class 装载机的主要优点是您可以根据您的要求控制 class 装载机制。

java.lang.ClassLoader to define own customized class loader. java.lang.ClassLoader定义自己定制的 class 加载器。 Every class loader in JAVA should be child class of java.lang.ClassLoader class, either directly of indirectly. 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.因此,此 class 充当所有定制 class 装载机的基础 class。

Note: While designing/developing web servers and application server, usually customized class loaders are used to customized class loading mechanism.注意:在设计/开发 web 服务器和应用服务器时,通常使用定制的 class 加载器来定制 class 加载机制。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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