获取服务器classes根路径

/**
    *     获取web应用路径
    *  @Description    : 方法描述
    *  @Method_Name    : getRootPath
    *  @return 
    *  @return         : String
    *  @Creation Date  : 2013-12-13 下午9:09:28 
    *  @version        : v1.00
    * @throws UnsupportedEncodingException 
    *  @Author         : zyn
    
    *  @Update Date    : 
    *  @Update Author  : zyn
    */
    public static String getRootPath() throws UnsupportedEncodingException {
//        String classPath = Object.class.getClass().getResource("/").getPath();
        String classPath = WebHelper.class.getClassLoader().getResource("/").getPath();//服务器启动才好用
//        "/" 才是class 根目录
        String rootPath = "";
        // windows下
        if ("\".equals(File.separator)) {
            rootPath = classPath.substring(1, classPath.indexOf("/WEB-INF/classes"));
            rootPath = rootPath.replace("/", "\");
            rootPath = URLDecoder.decode(rootPath, "UTF-8");
        }
        // linux下
        if ("/".equals(File.separator)) {
            rootPath = classPath.substring(0, classPath.indexOf("/WEB-INF/classes"));
            rootPath = rootPath.replace("\", "/");
            rootPath = URLDecoder.decode(rootPath, "UTF-8");
        }
        return rootPath;
    }
原文地址:https://www.cnblogs.com/sallet/p/3941582.html