Java获取当前类路径

时间:2022-08-26 16:04:17


public class PathTest {
/**
* @date 2012-7-11
* @param args
*/
public static void main(String[] args) {
String path = PathTest.class.getResource("/").getPath();
System.out.println(path);
path = PathTest.class.getResource("").getPath();
System.out.println(path);
}
}

输出结果:

/E:/WorkSpace/MyTest/
/E:/WorkSpace/MyTest/lmiky/test/clazz/

有加上"/"获取的是根路径,如果是tomcat的web项目,则获取的是classes路径