this.getClass (). getResource (String name) .getPath () Gets the classpath (class path) is stored peer file path .class compiled

    @Test
    public void run() {
        // getResource(String name) 得到的是一个url对象来定位资源
        String path1 = this.getClass().getResource("").getPath();   // 不用'/'开头,是获取当前类编译后所对应的.class文件的目录  
        String path2 = this.getClass().getResource("/").getPath();  // 用'/'开头,则是获取classpath路径(存放.class文件的根目录),就是当前项目的.class文件根目录  
        System.out.println("path1 = " + path1); // path1 = /E:/xcEduOnline/xcEduService01/test-freemarker/target/test-classes/com/xuecheng/test/freemarker/
        System.out.println("path2 = " + path2); // path2 = /E:/xcEduOnline/xcEduService01/test-freemarker/target/test-classes/
    }

 

Published 23 original articles · won praise 6 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42046751/article/details/102018918