你应该在tomcat目录下找你的class文件的目录,那是你运行时候的目录,然后对应找你文件的目录就好找了

解决方案 »

  1.   

    大哥,我是在Eclipse下建的项目,classes文件肯定在我的Eclipse项目文件夹下,至于我做完后打包放到Tomcat还是Weblogic或者Bes都是没有关系的啊。你所说的相对于Classes文件夹的路径不就是我上面提到的第二种方法么?那已经证明不行,关键是它默认的根路径是D:\Eclipse,这是怎么回事?我觉得应该是系统设置问题。哪位高人能正确解答这个问题呢?难道CSDN的java板块这么没人气么?
      

  2.   

    String className = "org.apache.commons.dbcp.BasicDataSource";
         String classNameUrl="/org/apache/commons/dbcp/BasicDataSource.class";
         String temp=null;
            int i = 0;
            try {
             Class.forName(className);        
                java.net.URL classUrl = FilesUtil.class
                        .getResource(classNameUrl);
                temp = classUrl.getPath();
                temp = URLDecoder.decode(temp);
                if ("Win".equalsIgnoreCase(System.getProperty("os.name").substring(
                        0, 3))) {
                    i = 6;
                } else {
                    i = 5;
                }
                webRoot = temp.substring(i, temp.indexOf("WEB-INF") - 1);
            } catch (Exception e) {
             UlandLogger.debug("取得webRoot时出错:", e);
             webRoot = "";
            }是取到目录为webRoot
    有了这个目录web应用中所有目录就ok
    如我自己的:
    /**
     * 取到目录为webRoot/xml/
     * 
     * @return
     */
    public static String getXmlPath(){
    // 取到webRoot/xml/
    String path= getWebRoot()+File.separator+"xml"+File.separator;
    return path;
    }
    /**
     * 取到目录为webRoot/xml/
     * 
     * @return
     */
    public static String getConfigPath(){
    // 取到webRoot/xml/
    String path= getWebRoot()+File.separator+"config"+File.separator;
    return path;
    }
      

  3.   

    to: ljhyp_cn(香草山牧民)
    多谢你了
    我用了session.getServletContext().getRealPath()  可以了,你那个有点复杂,不过还是要谢谢你哈!晚上下班后揭帖
      

  4.   

    这个东西,有时候不稳定.session.getServletContext().getRealPath()  会根据网站配置不同,出现问题.
    用我说的那样准确.当然,看你了!