public class Path
{
private static String homePath;
public static String getHome()
{
String path = System.getProperty("test.home");
if (path != null) {
return path;
}
String homePath = getProjectPath();
System.setProperty("test.home",homePath);
return homePath;
}
public static String getClassPath(){
String path = System.getProperty("test_class.home");
if (path != null) {
return path;
}
String homePath = new SysConfigPath().getClass().getResource("").getFile();
String packpath = "aygl_export/";
int index = homePath.lastIndexOf(packpath);
homePath = homePath.substring(0, index);
System.setProperty("test_class.home",homePath);
return homePath;
}
private static String getProjectPath(){
String homePath = getClassPath();
int index = homePath.lastIndexOf(".jar!/");
if (index != -1){
homePath = homePath.substring(0, homePath.length()-1);
index = homePath.lastIndexOf("/");
homePath = homePath.substring(0, index+1);
homePath = homePath.substring(5);
}
homePath = homePath.substring(0, homePath.length()-1);
index = homePath.lastIndexOf("/");
homePath = homePath.substring(0, index+1);
return homePath;
}
}
此博客展示了Java代码,定义了Path类,包含获取主路径、类路径和项目路径的方法。通过系统属性判断路径是否为空,若为空则进行相应处理,如截取字符串等操作来获取正确路径,还考虑了jar包的情况。
2515

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



