java、java Web 获取项目根路径

来源:互联网 发布:河南民生网络电视台 编辑:程序博客网 时间:2024/05/16 06:14

1、Java项目获取项目路径:

        //classes的文件根路径:/E:/workspace/gfdgdfds/build/classes/        System.out.println(this.getClass().getResource("/").getPath());                //当前class文件的路径:/E:/workspace/gfdgdfds/build/classes/zhua_qu_shu_qu/        System.out.println(this.getClass().getResource("").getPath());                //项目的根路径:E:\workspace\gfdgdfds        System.out.println(System.getProperty("user.dir") );                //项目的根路径:E:\workspace\gfdgdfds        File directory = new File("");// 参数为空        String courseFile = directory.getCanonicalPath();        System.out.println(courseFile);

2、Java Web 项目获取项目路径(获取的Tomcat中的项目路径)

    //项目的根路径:E:\apache-tomcat-7.0.57\webapps\test    System.out.println(request.getSession().getServletContext().getRealPath(""));        //当前的请求路径:/test/login.action    System.out.println(request.getRequestURI());        //项目的部署名称:/test    System.out.println(request.getContextPath());        //classes的文件根路径:/E:/apache-tomcat-7.0.57/webapps/test/WEB-INF/classes/    System.out.println(this.getClass().getResource("/").getPath());




原创粉丝点击