java获取当前项目或类路径

         File directory = new File("");// 参数为空
         String courseFile = directory.getCanonicalPath();
         System.out.println(courseFile);//注意返回的是反斜杠标识的目录名
// 获取当前类的目录
         URL xmlpath = this.getClass().getClassLoader().getResource("");
         System.out.println(xmlpath);
      File f = new File(this.getClass().getResource("/").getPath());
         System.out.println(f);
// 获取所有的类路径 包括jar包的路径
         System.out.println(System.getProperty("java.class.path"));

需要处理异常

猜你喜欢

转载自www.cnblogs.com/Pusteblume/p/10202625.html