您好,欢迎访问一九零五行业门户网

java中获取类加载路径和项目根路径的5种方式分析

package my; import java.io.file; import java.io.ioexception; import java.net.url; public class myurldemo { public static void main(string[] args) { myurldemo mudemo = new myurldemo(); try { mudemo.showurl(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } public void showurl() throws ioexception { // 第一种:获取类加载的根路径 d:\git\daotie\daotie\target\classes file f = new file(this.getclass().getresource("/").getpath()); system.out.println(f); // 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录 d:\git\daotie\daotie\target\classes\my file f2 = new file(this.getclass().getresource("").getpath()); system.out.println(f2); // 第二种:获取项目路径 d:\git\daotie\daotie file directory = new file("");// 参数为空 string coursefile = directory.getcanonicalpath(); system.out.println(coursefile); // 第三种: file:/d:/git/daotie/daotie/target/classes/ url xmlpath = this.getclass().getclassloader().getresource(""); system.out.println(xmlpath); // 第四种: d:\git\daotie\daotie system.out.println(system.getproperty("user.dir")); /* * 结果: c:\documents and settings\administrator\workspace\projectname * 获取当前工程路径 */ // 第五种: 获取所有的类路径 包括jar包的路径 system.out.println(system.getproperty("java.class.path")); } }
更多java中获取类加载路径和项目根路径的5种方式分析。
其它类似信息

推荐信息