1、类加载器加载路径
public class MyTest18 { public static void main(String[] args) { //系统类加载器加载路径 System.out.println(System.getProperty("sun.boot.class.path")); System.out.println(); //扩展类加载器加载路径 System.out.println(System.getProperty("java.ext.dirs")); System.out.println(); //应用类加载器加载路径 System.out.println(System.getProperty("java.class.path")); } }
返回结果
C:Program FilesJavajdk1.8.0_131jrelib esources.jar;C:Program FilesJavajdk1.8.0_131jrelib t.jar;C:Program FilesJavajdk1.8.0_131jrelibsunrsasign.jar;C:Program FilesJavajdk1.8.0_131jrelibjsse.jar;C:Program FilesJavajdk1.8.0_131jrelibjce.jar;C:Program FilesJavajdk1.8.0_131jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_131jrelibjfr.jar;C:Program FilesJavajdk1.8.0_131jreclasses C:Program FilesJavajdk1.8.0_131jrelibext;C:WindowsSunJavalibext C:Program FilesJavajdk1.8.0_131jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_131jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_131jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_131jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_131jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_131jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_131jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_131jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_131jrelibext ashorn.jar;C:Program FilesJavajdk1.8.0_131jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_131jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_131jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_131jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_131jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_131jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_131jrelibjce.jar;C:Program FilesJavajdk1.8.0_131jrelibjfr.jar;C:Program FilesJavajdk1.8.0_131jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_131jrelibjsse.jar;C:Program FilesJavajdk1.8.0_131jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_131jrelibplugin.jar;C:Program FilesJavajdk1.8.0_131jrelib esources.jar;C:Program FilesJavajdk1.8.0_131jrelib t.jar;D:workspacestudy jvm_demouildclassesjavamain;D:Program FilesJetBrainsIntelliJ IDEA 2017.1.4libidea_rt.jar
将打印结果变成多行数据
C:Program FilesJavajdk1.8.0_131jrelib esources.jar; C:Program FilesJavajdk1.8.0_131jrelib t.jar; C:Program FilesJavajdk1.8.0_131jrelibsunrsasign.jar; C:Program FilesJavajdk1.8.0_131jrelibjsse.jar; C:Program FilesJavajdk1.8.0_131jrelibjce.jar; C:Program FilesJavajdk1.8.0_131jrelibcharsets.jar; C:Program FilesJavajdk1.8.0_131jrelibjfr.jar; C:Program FilesJavajdk1.8.0_131jreclasses C:Program FilesJavajdk1.8.0_131jrelibext;C:WindowsSunJavalibext C:Program FilesJavajdk1.8.0_131jrelibcharsets.jar; C:Program FilesJavajdk1.8.0_131jrelibdeploy.jar; C:Program FilesJavajdk1.8.0_131jrelibextaccess-bridge-64.jar; C:Program FilesJavajdk1.8.0_131jrelibextcldrdata.jar; C:Program FilesJavajdk1.8.0_131jrelibextdnsns.jar; C:Program FilesJavajdk1.8.0_131jrelibextjaccess.jar; C:Program FilesJavajdk1.8.0_131jrelibextjfxrt.jar; C:Program FilesJavajdk1.8.0_131jrelibextlocaledata.jar; C:Program FilesJavajdk1.8.0_131jrelibext ashorn.jar; C:Program FilesJavajdk1.8.0_131jrelibextsunec.jar; C:Program FilesJavajdk1.8.0_131jrelibextsunjce_provider.jar; C:Program FilesJavajdk1.8.0_131jrelibextsunmscapi.jar; C:Program FilesJavajdk1.8.0_131jrelibextsunpkcs11.jar; C:Program FilesJavajdk1.8.0_131jrelibextzipfs.jar; C:Program FilesJavajdk1.8.0_131jrelibjavaws.jar; C:Program FilesJavajdk1.8.0_131jrelibjce.jar; C:Program FilesJavajdk1.8.0_131jrelibjfr.jar; C:Program FilesJavajdk1.8.0_131jrelibjfxswt.jar; C:Program FilesJavajdk1.8.0_131jrelibjsse.jar; C:Program FilesJavajdk1.8.0_131jrelibmanagement-agent.jar; C:Program FilesJavajdk1.8.0_131jrelibplugin.jar; C:Program FilesJavajdk1.8.0_131jrelib esources.jar; C:Program FilesJavajdk1.8.0_131jrelib t.jar; D:workspacestudy jvm_demouildclassesjavamain; D:Program FilesJetBrainsIntelliJ IDEA 2017.1.4libidea_rt.jar
2、修改类加载器启动类加载器
现在想把自己生成的类交给启动类加载器,那要怎么做呢?
主需要把生成的class文件放到根加载器的加载路径下 C:Program FilesJavajdk1.
8
.0_131jreclasses
创建测试类
public class MyTest18_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.MyTest1"); System.out.println("class: " +clazz.hashCode()); System.out.println("class loader: " + clazz.getClassLoader()); } }
运行结果如下:
class: 356573597 class loader: null
说明是启动加载器加载的MyTest1