获取根目录五种方法

//第一种
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists()) path = new File("");
System.out.println("=== " + path.getAbsolutePath());
//第二种
System.out.println("=== " + System.getProperty("user.dir"));
//第三种
String path1 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
System.out.println("=== " + URLDecoder.decode(path1, "utf-8"));
//第四种
String path2 = ResourceUtils.getURL("classpath:").getPath();
System.out.println("=== " + path2);
//第五种
ApplicationHome h = new ApplicationHome(RuoYiApplication.class);
File jarF = h.getSource();
System.out.println("=== " + jarF.getParentFile().toString() + "\\(^o^)/");


Linux下Jar启动使用第五种方法

ApplicationHome h = new ApplicationHome(RuoYiApplication.class);
File jarF = h.getSource();
System.out.println("=== " + jarF.getParentFile().toString() + "\\(^o^)/");

由于系统及环境原因,每种方法还是有差异;

我们的项目部署在CentOS中,最后选用第五种方法。