Java动态类加载机制应用研究

2018-09-13 10:47:16孙彬
科技创新与应用 2018年23期

孙彬

摘 要:在Java应用程序运行时,Java虚拟机中的类加载机制能在不影响其他功能模块运行的情况下动态加载类文件。文章简述了Java类加载器的层次体系结构以及动态加载机制原理,并结合具体的例子,将Java动态加载机制应用在高校网站管理平台的文件发布模块,实现了系统和静态网页的关联,可快速搭建二级网站,还能通过自定义加载器能对加密的字节码进行解密,得出了Java语言具有强大的灵活性和安全度,能很好的保护代码安全。

关键词:Java;动态类加载机制;结构体系

中图分类号:TP312 文献标志码:A 文章编号:2095-2945(2018)23-0180-02

Abstract: The class-loading mechanism in the Java Virtual Machine can dynamically load class files without affecting the running of other functional modules while the Java application is running. In this paper, the hierarchical structure of Java classloader and the principle of dynamic loading mechanism are briefly described. Based on specific examples, Java dynamic loading mechanism is applied to file publishing module of university website management platform. The association between the system and the static web page is realized, the secondary web site can be built quickly, and the encrypted bytecode can be decrypted by the custom loader. It is concluded that the Java language has strong flexibility and security, and can protect the code security very well.

Keywords: Java; dynamic classloading mechanism; architecture system

动态加载能在运行时安装程序组件,Microsoft Windows等系统中都有用到动态加载技术。动态连接比静态连接更有灵活性,并且能节约一定的内存空间。Java动态类加载是调用所用的类,这些类并未在源代码中提到,单个程序是由多个类组成的。一个程序启动时总是先加载一些基础类到JVM,剩余的类是在需要时再另行加载的。这种加载方式能在很大程度上实现对内存的节约,体现Java语言加载的动态性。Java体系结构语言包括了Java语言、Java虚拟机4大部分、Java class文件和Java API,其中JVM是Java体系结构的核心部分[1]。Java动态类加载先将程序运行的基础类加载到JVM中,等JVM用到时再加载其他类。

1 Java类加载器的结构体系

Java中的类需被装载到JVM中实现运行,JVM中的有类加载器,负责完成对Java类的装载。类加载器从硬盘中读取类文件到内存里。Java中的类的形式多种多样,根据功能和加载方式的不同,基本上可分为核心类、扩展类还要许多用户根据自身需求自定义的类。……

登录APP查看全文