首页 > 编程语言 > 详细

Java获取项目路径及classes路径

时间:2018-08-10 19:29:29      阅读:256      评论:0      收藏:0      [点我收藏+]
以工程名为test为例

1.获取项目绝对路径一

String rootPath = request.getSession().getServletContext().getRealPath("/").replace("\\", "/");
获取内容如下:
D:/apacheTomcat/apache-tomcat-7.0.59/webapps/test/

2.获取web工程名

String projectName = request.getContextPath();
获取内容如下:
/test

3.获取classes路径,最后的.substring(1)是为了去掉前面“/”

public static String getClassesPath(){
    String classesPath=Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
    return classesPath;
}

获取内容如下:
D:/apacheTomcat/apache-tomcat-7.0.59/webapps/test/WEB-INF/classes/

4.获取项目绝对路径二

private static String getProjectRootPath(){
    String rootPath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
    rootPath = rootPath.substring(1,rootPath.indexOf("WEB-INF"));
    return rootPath;
}

获取内容如下:
D:/apacheTomcat/apache-tomcat-7.0.59/webapps/test/

Java获取项目路径及classes路径

原文:http://blog.51cto.com/1197822/2157270

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!