Java教程

Java类加载器

本文主要是介绍Java类加载器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

ClassLoader的分类

Java中的ClassLoader有三种:Bootstrap ClassLoader 、Extension ClassLoader、App ClassLoader。以下是jdk1.8的类加载器

ClassLoader结构

 加载路径

package com.example.demo;

import java.net.URL;
import java.net.URLClassLoader;

public class Test {
    public static void main(String[] args) {
        System.out.println("BootstrapClassLoader 的加载路径: ");

        //String[] bootUrls = System.getProperty("sun.boot.class.path").split(";");

        URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
        for (URL url : urls)
            System.out.println(url);
        System.out.println("----------------------------");

        //取得扩展类加载器
        URLClassLoader extClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader().getParent();

        System.out.println(extClassLoader);
        System.out.println("扩展类加载器 的加载路径: ");

        urls = extClassLoader.getURLs();
        for (URL url : urls)
            System.out.println(url);

        System.out.println("----------------------------");


        //取得应用(系统)类加载器
        URLClassLoader appClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

        System.out.println(appClassLoader);
        System.out.println("应用(系统)类加载器 的加载路径: ");

        urls = appClassLoader.getURLs();
        for (URL url : urls)
            System.out.println(url);

        System.out.println("----------------------------");
    }

}

运行结果如下:

BootstrapClassLoader 的加载路径: 
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/resources.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/rt.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/sunrsasign.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jsse.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jce.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/charsets.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jfr.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/classes
----------------------------
sun.misc.Launcher$ExtClassLoader@776ec8df
扩展类加载器 的加载路径: 
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/access-bridge-64.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/cldrdata.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/dnsns.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/jaccess.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/jfxrt.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/localedata.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/nashorn.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunec.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunjce_provider.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunmscapi.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunpkcs11.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/zipfs.jar
----------------------------
sun.misc.Launcher$AppClassLoader@18b4aac2
应用(系统)类加载器 的加载路径: 
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/charsets.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/deploy.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/access-bridge-64.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/cldrdata.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/dnsns.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/jaccess.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/jfxrt.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/localedata.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/nashorn.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunec.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunjce_provider.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunmscapi.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/sunpkcs11.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/ext/zipfs.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/javaws.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jce.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jfr.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jfxswt.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/jsse.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/management-agent.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/plugin.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/resources.jar
file:/F:/Program%20Files/Java/jdk1.8.0_111/jre/lib/rt.jar
file:/F:/myproject/demo/target/classes/

自定义类加载器
 

package com.example.demo;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException {
        String classPath = "F:/";
        CustomerClassLoader myClassLoader = new CustomerClassLoader(classPath);
        String packageNamePath = "LogTest";
        Class logTest = myClassLoader.loadClass(packageNamePath);
        System.out.println("classloader is :" + logTest.getClassLoader().getClass().getName());
    }
}
package com.example.demo;

import java.io.FileInputStream;

public class CustomerClassLoader extends ClassLoader {
    private String loadPath;

    public CustomerClassLoader(String loadPath) {
        this.loadPath = loadPath;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            byte[] bytes = loadByte(name);
            return defineClass(name, bytes, 0, bytes.length);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private byte[] loadByte(String className) throws Exception {
        className = className.replaceAll("\\.", "/");
        FileInputStream fileInputStream = new FileInputStream(loadPath + "/" + className + ".class");
        int available = fileInputStream.available();
        byte[] data = new byte[available];
        fileInputStream.read(data);
        fileInputStream.close();
        return data;
    }
}

这篇关于Java类加载器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!