Java 获取web项目根目录 RootPath

public class Path
{
 public static String getRootPath(){
  String path = null;
   try {
      path = Path.class.getResource("/").toURI().getPath();
      path = path.substring(1, path.length()-1);
      String str = "WEB-INF/classes";
      int i = path.indexOf(str);
      if(i > 0){
       path = path.substring(0, i);
      }
   } catch (Exception e) {
      e.printStackTrace();
   }
  return path;
 }
}

你可能感兴趣的:(Java,根目录,RootPath,Java)