this.getClass().getResource(String name).getPath() 获取classpath路径(类路径),就是存放.class等编译后的文件路径

    @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/
    }
发布了23 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42046751/article/details/102018918
今日推荐