一、自定义类加载器在复杂类情况下的运行分析

1、使用之前创建的类加载器

public class MyTest16  extends  ClassLoader{

    private String className;

    //目录
private String path; private final String fileExtension = ".class"; public MyTest16(String classLoadName){
super(); //将系统类加载器当做该类加载器的父加载器
this.className = classLoadName;
} public MyTest16(ClassLoader parent, String classLoadName){
super(parent); //显示指定该类加载器的父加载器器
this.className = classLoadName;
} public void setPath(String path) {
this.path = path;
} @Override
public String toString() {
return "[" + this.className + "]";
} @Override
protected Class<?> findClass(String clasName) throws ClassNotFoundException {
System.out.println("findClass invoked:" + clasName);
System.out.println("class loader name: " + this.className);
byte[] data = this.loadClassData(clasName);
return this.defineClass(clasName,data, 0, data.length);
} private byte[] loadClassData(String className){
InputStream is = null;
byte[] data = null;
ByteArrayOutputStream baos = null; try{
className = className.replace(".","//");
//System.out.println("className:" +this.className);
is = new FileInputStream(new File(this.path + className + this.fileExtension));
baos = new ByteArrayOutputStream();
int ch = 0;
while ( -1 != (ch = is.read())){
baos.write(ch);
}
data = baos.toByteArray(); }catch (Exception ex){
ex.printStackTrace();
}finally {
try {
is.close();
baos.close();
}catch (Exception ex){
ex.printStackTrace();
}
} return data; } }

  

2、创建MyCat类

public class MyCat {
public MyCat(){
System.out.println("MyCat is loaded by:" + this.getClass().getClassLoader());
}
}

  

3、创建MySample类

public class MySample {

    public MySample(){
System.out.println("MySample is loaded by:" + this.getClass().getClassLoader());
new MyCat(); }
}

  

4、创建测试类

public class MyTest17 {
public static void main(String[] args) throws Exception{
MyTest16 loader1 = new MyTest16("loader1"); Class<?> clazz = loader1.loadClass("com.example.jvm.classloader.MySample"); System.out.println("class:" + clazz.hashCode()); //如果注释掉改行,那么并不会实例化MySample对象,即MySample构造方法不会被调用
//因此不会实例化MyCat对象,即没有对MyCat进行主动使用,这里就不会加重MyCat Class
Object object = clazz.newInstance(); }
}

  

打印结果

class:1735600054
MySample is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
MyCat is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2

  

增加-XX:+TraceClassLoading后的打印结果

如果去掉Object object = clazz.newInstance();

打印结果为

说明:如果注释掉Object object = clazz.newInstance();该行,那么并不会实例化MySample对象,即MySample构造方法不会被调用

因此不会实例化MyCat对象,即没有对MyCat进行主动使用,这里就不会加载MyCat Class。

         而且这个例子说明MyCat没有被预先加载

二、对一的Code进行改造

在一的基础上,新建一个测试类

public class MyTest17_1 {
public static void main(String[] args) throws Exception{
MyTest16 loader1 = new MyTest16("loader1");
loader1.setPath("D:/temp/");
Class<?> clazz = loader1.loadClass("com.example.jvm.classloader.MySample"); System.out.println("class:" + clazz.hashCode()); //如果注释掉该行,那么并不会实例化MySample对象,即MySample构造方法不会被调用
//因此不会实例化MyCat对象,即没有对MyCat进行主动使用,这里就不会加载MyCat Class
Object object = clazz.newInstance(); }
}

  里面增加了一个方法loader1.setPath("D:/temp/");

然后将MyCat.class 和MySample.class 剪切到D:/temp/目录下,如下面两图

打印结果:

findClass invoked:com.example.jvm.classloader.MySample
class loader name: loader1
class:2133927002
MySample is loaded by:[loader1]
findClass invoked:com.example.jvm.classloader.MyCat
class loader name: loader1
MyCat is loaded by:[loader1]

  

三、类加载命名空间

在二的基础上,MyCat.java 增加一行代码System.out.println("from MyCat:" + MySample.class);

public class MyCat {
public MyCat(){
System.out.println("MyCat is loaded by:" + this.getClass().getClassLoader()); System.out.println("from MyCat:" + MySample.class);
}
}

  然后重新build,打印结果如下:

class:1735600054
MySample is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
MyCat is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
from MyCat:class com.example.jvm.classloader.MySample

  

现在将build路径下的classloader文件夹拷贝到D:\temp\com\example\jvm\classloader下,删除build路径下的MySample.class

打印输出结果:

findClass invoked:com.example.jvm.classloader.MySample
class loader name: loader1
class:2133927002
MySample is loaded by:[loader1]
MyCat is loaded by:sun.misc.Launcher$AppClassLoader@18b4aac2
Exception in thread "main" java.lang.NoClassDefFoundError: com/example/jvm/classloader/MySample
at com.example.jvm.classloader.MyCat.<init>(MyCat.java:10)
at com.example.jvm.classloader.MySample.<init>(MySample.java:10)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at com.example.jvm.classloader.MyTest17_1.main(MyTest17_1.java:16)
Caused by: java.lang.ClassNotFoundException: com.example.jvm.classloader.MySample
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 8 more

  说明:MySample由自定义loader加载,MyCat由AppClassLoader。两个类是不同的loader加载,处于不同的命名空间。

所以System.out.println("from MyCat:" + MySample.class); 抛出异常。 子加载器所加载的类能够访问父加载器所加载的类。反之,父加载器所加载的类无法访问子加载器所加载的类

  

JVM 自定义类加载器在复杂类情况下的运行分析的更多相关文章

  1. JVM自定义类加载器加载指定classPath下的所有class及jar

    一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...

  2. (转)JVM——自定义类加载器

    背景:为什么要自定义,如何自定义,实现过程 转载:http://blog.csdn.net/SEU_Calvin/article/details/52315125 0. 为什么需要自定义类加载器 网上 ...

  3. JVM——自定义类加载器

    )以上两种情况在实际中的综合运用:比如你的应用需要通过网络来传输 Java 类的字节码,为了安全性,这些字节码经过了加密处理.这个时候你就需要自定义类加载器来从某个网络地址上读取加密后的字节代码,接着 ...

  4. JVM 自定义类加载器

    一.创建自定义类加载器 package com.example.jvm.classloader; import java.io.ByteArrayOutputStream; import java.i ...

  5. 4.自定义类加载器实现及在tomcat中的应用

    了解了类加载器的双亲委派机制, 也知道了双亲委派机制的原理,接下来就是检验我们学习是否扎实了,来自定义一个类加载器 一. 回顾类加载器的原理 还是这张图,类加载器的入口是c++调用java代码创建了J ...

  6. jvm(1)类的加载(二)(自定义类加载器)

    [深入Java虚拟机]之四:类加载机制 1,从Java虚拟机的角度,只存在两种不同的类加载器: 1,启动类加载器:它使用C++实现(这里仅限于Hotspot,也就是JDK1.5之后默认的虚拟机,有其他 ...

  7. Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论

    Java虚拟机JVM学习06 自定义类加载器 父委托机制和命名空间的再讨论 创建用户自定义的类加载器 要创建用户自定义的类加载器,只需要扩展java.lang.ClassLoader类,然后覆盖它的f ...

  8. JVM性能优化--类加载器,手动实现类的热加载

    一.类加载的机制的层次结构 每个编写的".java"拓展名类文件都存储着需要执行的程序逻辑,这些".java"文件经过Java编译器编译成拓展名为". ...

  9. 【Java虚拟机8】自定义类加载器、类加载器命名空间、类的卸载

    前言 学习类加载器就一定要自己实现一个类加载器,今天就从一个简单的自定义类加载器说起. 自定义类加载器 例1 一个简单的类加载器,从一个给定的二进制名字读取一个字节码文件的内容,然后生成对应的clas ...

随机推荐

  1. uWSGI+django+nginx的工作原理流程与部署历程

    一.前言献给和我一样懵懂中不断汲取知识,进步的人们. 霓虹闪烁,但人们真正需要的,只是一个可以照亮前路的烛光 二.必要的前提2.1 准备知识 django一个基于python的开源web框架,请确保自 ...

  2. chrome DevTools功能介绍

  3. @RequestMapping中的注解

    在org.springframework.spring-web的jar包中在以下层级下: org.springframework.web.bind.annotation; // // Source c ...

  4. DNS服务概念

    DNS: Domain Name Service域名:www.magedu.com(主机名,FQDN:Full Qualified Domain Name, 完全限定域名)DNS:名称解析,Name ...

  5. P1052 过河[DP]

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  6. vscode beautiful配置

    在工作目录下建立.jsbeautifyrc文件 官方文档 { "brace_style": "none,preserve-inline", "inde ...

  7. 关于Spring的常用注解和接口

    接口 1. BeanPostProcessor 2. FactoryBean 3. Condition 4. ImportSelector 5. ImportBeanDefinitionRegitra ...

  8. win32gui.EnumWindows

    python2 import win32gui, win32con, win32api import time, math, random def _MyCallback( hwnd, extra ) ...

  9. LightOJ - 1410 - Consistent Verdicts(规律)

    链接: https://vjudge.net/problem/LightOJ-1410 题意: In a 2D plane N persons are standing and each of the ...

  10. Continuous Subarray Sum II

    Description Given an circular integer array (the next element of the last element is the first eleme ...