二、 普通项目中
String path = this.getClass().getClassLoader().getResource("").getPath();
二、 SpringBoot 中
框架提供了工具类可以 ResourceUtils.getURL("classpath:").getPath()获取类路径
@Test
public void t() throws FileNotFoundException {
URL url = ResourceUtils.getURL("classpath:");
//url = file:/C:/Users/12603/IdeaProjects/bookstore/target/test-classes/
System.out.println("url = " + url);
//path = /C:/Users/12603/IdeaProjects/bookstore/target/test-classes/
String path = url.getPath();
System.out.println("path = " + path);
}