在用SpringBoot进行路径调用时一直调用不到
这里讲一下
机制
这里我在xml文件运行出对应的路径就是整个项目文件
所以:可以认为你存在src文件夹中的都是在springboot-web-req-resp项目文件夹下的
然后
对应的resources下的emp.xml对应的路径是
"/D:/Java/Java-Code/springboot-web-req-resp/target/classes/emp.xml"
so
我们存在resource都会到对应的target里面classes
里面的结构是
就这种,来表示对于的路径关系
修改成相对路径
修改前
"/D:/Java/Java-Code/springboot-web-req-resp/target/classes/emp.xml"
修改后
因为xml.java
路径是
"/D:/Java/Java-Code/springboot-web-req-resp"
所以把"/D:/Java/Java-Code/springboot-web-req-resp/target/classes/emp.xml"
修改成"./target/classes/emp.xml"即可
修改后可以正常运行(可以查找到)
Java中获取类路径的几种方式
第一种:
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
结果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
获取当前类的所在工程路径;
如果不加“/”
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);
结果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
获取当前类的绝对路径;
第二种:
File directory = new File("");//参数为空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
结果:
C:\Documents and Settings\Administrator\workspace\projectName
获取当前类的所在工程路径;
第三种:(!!!!!!!!!!!!!!!!!!!!!!!!!!!重点记)
URL xmlpath = this.getClass().getClassLoader().getResource(“selected.txt”);
System.out.println(xmlpath);
结果:
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
获取当前工程src目录下selected.txt文件的路径
第四种:
System.out.println(System.getProperty(“user.dir”));
结果:
C:\Documents and Settings\Administrator\workspace\projectName
获取当前工程路径
第五种:
System.out.println( System.getProperty(“java.class.path”));
结果:
C:\Documents and Settings\Administrator\workspace\projectName\bin
获取当前工程路径
}