1、Java也提供很多方法来获取这些路径:
//获取src资源文件编译后的路径(即classes路径)
xxx.class.getClassLoader().getResource("").getPath();
//获取classes路径下“文件”的路径
xxx.class.getClassLoader().getResource("文件名").getPath();
//缺少类加载器,获取xxx类经编译后的xxx.class路径
xxx.class.getResource("").getPath();
//以上方法的另外一种写法
this.getClass().getClassLoader().getResource("").getPath();
this.getClass().getResource("").getPath();
2、输出上面路径:
System.out.println(xxx.class.getClassLoader().getResource("").getPath());
System.out.println(xxx.class.getClassLoader().getResource("conf.properties").getPath());
System.out.println(xxx.class.getResource("").getPath());
System.out.println(this.getClass().getClassLoader().getResource("").getPath());
输出依次为:
/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/
/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/conf.properties
/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/com/gmi/client/
/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/