java获取项目路径

1.user.dir     不稳定。不同系统可能获取的路径不一样。

System.getProperty("user.dir")

2.最好使用

this.getClass().getResource("/") //当字符串输出时,是带file:的,如果只需要路径,需要.getPath()
实例如下:
private  URL classpathUrl= this.getClass().getResource("/");
private String filePath =classpathUrl.getPath()+"log4j1.properties";
posted @ 2017-09-24 23:15  zipon  阅读(214)  评论(0)    收藏  举报