java classes 路径_Java获取项目路径及classes路径
1.獲取項(xiàng)目絕對(duì)路徑一
String rootPath = request.getSession().getServletContext().getRealPath("/").replace("\\", "/");
獲取內(nèi)容如下:
D:/apacheTomcat/apache-tomcat-7.0.59/webapps/test/
2.獲取web工程名
String projectName = request.getContextPath();
獲取內(nèi)容如下:
/test
3.獲取classes路徑,最后的.substring(1)是為了去掉前面“/”
public static String getClassesPath(){
String classesPath=Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
return classesPath;
}
獲取內(nèi)容如下:
D:/apacheTomcat/apache-tomcat-7.0.59/webapps/test/WEB-INF/classes/
4.獲取項(xiàng)目絕對(duì)路徑二
private static String getProjectRootPath(){
String rootPath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
rootPath = rootPath.substring(1,rootPath.indexOf("WEB-INF"));
return rootPath;
}
獲取內(nèi)容如下:
D:/apacheTomcat/apache-tomcat-7.0.59/webapps/test/
原文:http://blog.51cto.com/1197822/2157270
總結(jié)
以上是生活随笔為你收集整理的java classes 路径_Java获取项目路径及classes路径的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: docker 容器之间通信_四、Dock
- 下一篇: 计算机硬件系统教具,计算机硬件系统 (2