1.利用system类获取路径。
System.out.println(System.getProperty("user.dir"));输出是:
/home/jpan/workspace/test
项目名为test,即获得项目路径。
其中user.dir是system的property,可以在api中找到。其中与路径相关的property还有
user.name User's account name
user.home User's home directory
user.dir User's current working directory
2.利用File类获取路径
File f = new File(".");
System.out.println(f.getAbsolutePath()+"\n"+f.getCanonicalPath()+"\n"+f.getPath());输出是:
/home/jpan/workspace/test/.
/home/jpan/workspace/test
.
3.在tomcat中获取路径
在tomcat中用system.getProperty()获得的路径是
/home/jpan/Software/apache-tomcat-7.0.53/
即是tomcat的命令执行路径。
因为项目需要,需要获得项目的安装路径,于是自己写了个类。
public class GetPath {
private static String projectName= "test";
public String getProjectPath() {
String tomcatPath=System.getProperty("user.dir");
String temPath = "/webapps/"+projectName;
return temPath;
}
}
1487

被折叠的 条评论
为什么被折叠?



