ClassLoader-[Class.getResource与Class.getClassLoader().getResource的区别]

 

public class ClassLoaderTest {

    public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, IllegalAccessException, InstantiationException {

        try {
            //classloader的base目录指的是file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/com/mashao/study/classloader/
            //class.getResource("相对与classloader base目录的相对路径")
            //classloader.getResource("可以是相对路径也可以是绝对路径")

            //ClassLoaderTest类所在的当前目录
            //file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/com/mashao/study/classloader/
            System.out.println(ClassLoaderTest.class.getResource(""));
            //ClassLoaderTest类所在的当前目录
            //file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/com/mashao/study/classloader/
            System.out.println(ClassLoaderTest.class.getResource("./"));
            //ClassLoaderTest类所在的当前目录的上一级目录
            //file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/com/mashao/study/
            System.out.println(ClassLoaderTest.class.getResource("../"));
            //ClassLoaderTest类所在的根目录,也就是classLoader的base目录
            //file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/
            System.out.println(ClassLoaderTest.class.getResource("/"));
            //输出为ClassLoaderTest类所在的当前目录下的其他类:
            //file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/com/mashao/study/classloader/MyClass.class
            System.out.println(ClassLoaderTest.class.getResource("MyClass.class"));

            //ClassLoaderTest对应的classloader的base目录
            //file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/
            System.out.println(ClassLoaderTest.class.getClassLoader().getResource(""));
            //输出为null, 因为/表示的是一个绝对路径,且该绝对路径不属于classloader的base目录范围内
            //null
            System.out.println(ClassLoaderTest.class.getClassLoader().getResource("/"));
            //ClassLoaderTest对应的classloader的当前目录即base目录
            //file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/
            System.out.println(ClassLoaderTest.class.getClassLoader().getResource("./"));
            //输出为null, 因为../表示classloader base目录的上级目录,不在base目录范围内,所以输出null
            //null
            System.out.println(ClassLoaderTest.class.getClassLoader().getResource("../"));
            //输出为命中的第一个包含com目录的jar包, 因为内部实现是先从jarLoader中去找
            //jar:file:/System/Library/Java/Extensions/MRJToolkit.jar!/com
            System.out.println(ClassLoaderTest.class.getClassLoader().getResource("com"));
            //输出为classloader base目录的下级目录【com】的全路径:file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/com
            //file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/com
            System.out.println(ClassLoaderTest.class.getClassLoader().getResource("./com"));
            //输出为classloader的base目录下的具体某个class的文件:
            //file:/Users/ouyangshibo/Documents/work/intellj_workspace/study/target/classes/com/mashao/study/classloader/MyClass.class
            System.out.println(ClassLoaderTest.class.getClassLoader().getResource("./com/mashao/study/classloader/MyClass.class"));


        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

 

 

你可能感兴趣的:(Java进阶)