java项目路径问题

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;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值