java获取当前项目及类的路径方法

File f1 = new File(this.getClass().getResource("/").getPath());
System.out.println("获取当前类的所在工程路径"+f1);

File f2 = new File(this.getClass().getResource("").getPath());
System.out.println("获取当前类的绝对路径"+f2);

File directory = new File("");//参数为空
String courseFile = null;
try {
   courseFile = directory.getCanonicalPath();
} catch (IOException e) {
   e.printStackTrace();
}
System.out.println("获取当前类的所在工程路径"+courseFile);

URL xmlpath = new AccountManageController().getClass().getClassLoader().getResource("privateKey");
System.out.println("获取当前工程src目录下privateKey文件的路径 "+xmlpath);

System.out.println("获取当前工程路径 "+System.getProperty("user.dir"));

System.out.println("获取当前工程所有jar包路径 "+System.getProperty("java.class.path"));

猜你喜欢

转载自blog.csdn.net/qq_38114157/article/details/85070863