java获取项目路径
程序员文章站
2022-04-17 12:04:03
...
SpringBoot项目jar发布获取jar包所在目录路径
参考:https://blog.csdn.net/liangcha007/article/details/88526181
//第一种
File path0 = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path0.exists()) path0 = new File("");
System.out.println("第1="+path0.getAbsolutePath());
//E:\code\project\XX\webserviceswagger\target\classes
//第二种
System.out.println("第2="+System.getProperty("user.dir"));
//E:\code\project\XX\webserviceswagger
//第三种
String path1 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
System.out.println("第3="+ URLDecoder.decode(path1, "utf-8"));
///E:/code/project/XX/webserviceswagger/target/classes/
//第四种
String path2 = ResourceUtils.getURL("classpath:").getPath();
System.out.println("第4="+path2);
///E:/code/project/XX/webserviceswagger/target/classes/
//第五种
ApplicationHome h = new ApplicationHome(getClass());
File jarF = h.getSource();
System.out.println("第5="+jarF.getParentFile().toString());
//E:\code\project\XX\webserviceswagger\target
参考:https://blog.csdn.net/liangcha007/article/details/88526181
//第一种
File path0 = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path0.exists()) path0 = new File("");
System.out.println("第1="+path0.getAbsolutePath());
//E:\code\project\XX\webserviceswagger\target\classes
//第二种
System.out.println("第2="+System.getProperty("user.dir"));
//E:\code\project\XX\webserviceswagger
//第三种
String path1 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
System.out.println("第3="+ URLDecoder.decode(path1, "utf-8"));
///E:/code/project/XX/webserviceswagger/target/classes/
//第四种
String path2 = ResourceUtils.getURL("classpath:").getPath();
System.out.println("第4="+path2);
///E:/code/project/XX/webserviceswagger/target/classes/
//第五种
ApplicationHome h = new ApplicationHome(getClass());
File jarF = h.getSource();
System.out.println("第5="+jarF.getParentFile().toString());
//E:\code\project\XX\webserviceswagger\target