1. @Test
    2. public void url() throws IOException {
    3. // 获取当前项目下路径:方式一
    4. File file = new File("");
    5. String filePath = file.getCanonicalPath();
    6. // 输出:D:\MyCode\log\log4j2-java
    7. System.out.println(filePath);
    8. // 获取当前项目下路径:方式二
    9. // 输出:D:\MyCode\log\log4j2-java
    10. System.out.println(System.getProperty("user.dir"));
    11. // 获取类加载的根路径
    12. String path = this.getClass().getResource("/").getPath().substring(1);
    13. // 输出:D:/MyCode/log/log4j2-java/target/classes/
    14. System.out.println(path);
    15. // 获取当前类的所在工程路径
    16. String path1 = this.getClass().getResource("").getPath().substring(1);
    17. // 输出:D:/MyCode/log/log4j2-java/target/classes/com/gzl/cn/
    18. System.out.println(path1);
    19. // 获取类加载指定文件或者文件夹路径
    20. String path2 = this.getClass().getResource("/data/DataConvert.dll").getPath().substring(1);
    21. System.out.println(path2);
    22. //获取所有的类路径 包括jar包的路径
    23. System.out.println(System.getProperty("java.class.path"));
    24. }