java获取当前工程目录

来源:互联网 发布:支持5g网络的手机 编辑:程序博客网 时间:2024/05/22 22:13
public class PathUtil {public static void main(String[] args)throws Exception {   PathUtil p = new PathUtil();   System.out.println(p.getWebClassesPath());   System.out.println(p.getWebInfPath());   System.out.println(p.getWebRoot());}public String getWebClassesPath() {   String path = getClass().getProtectionDomain().getCodeSource()     .getLocation().getPath();   return path;  }public String getWebInfPath() throws IllegalAccessException{   String path = getWebClassesPath();   if (path.indexOf("WEB-INF") > 0) {    path = path.substring(0, path.indexOf("WEB-INF")+8);   } else {    throw new IllegalAccessException("路径获取错误");   }   return path;}public String getWebRoot() throws IllegalAccessException{   String path = getWebClassesPath();   if (path.indexOf("WEB-INF") > 0) {    path = path.substring(0, path.indexOf("WEB-INF/classes"));   } else {    throw new IllegalAccessException("路径获取错误");   }   return path;}} 

要在项目启动后进行获取,直接执行main方法得不到WEB-INF所在的路经
原创粉丝点击