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 Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\classes C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext;C:\Windows\Sun\Java\lib\ext C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;D:\workspace\study\ jvm_demo\build\classes\java\main;D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.4\lib\idea_rt.jar
将打印结果变成多行数据
C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\sunrsasign.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar; C:\Program Files\Java\jdk1.8.0_131\jre\classes C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext;C:\Windows\Sun\Java\lib\ext C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar; C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar; D:\workspace\study\ jvm_demo\build\classes\java\main; D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.4\lib\idea_rt.jar
2、修改类加载器启动类加载器
现在想把自己生成的类交给启动类加载器,那要怎么做呢?
主需要把生成的class文件放到根加载器的加载路径下 C:\Program Files\Java\jdk1.
8
.0_131\jre\classes
创建测试类
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
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!