zoukankan      html  css  js  c++  java
  • IDEA中获取类加载路径和项目根路径

    /**
             * 第一种:获取类加载的根路径  D:WorkIdeaProjectsHelloVelocity	argetclasses
             */
            File f = new File(ControllerUtils.class.getClass().getResource("/").getPath());
            System.out.println("第一种:获取类加载的根路径");
            System.out.println(f);
    
            // 获取当前类的所在工程路径; 如果不加“/”获取当前类的加载目录  D:IdeaProjectscdby_wanWebRootWEB-INFclasses	estcom
            File f2 = new File(ControllerUtils.class.getClass().getResource("").getPath());
            System.out.println(f2);
    
    
            /**
             * 第二种:获取项目路径 D:WorkIdeaProjectsHelloVelocity
             */
            File directory = new File("");// 参数为空
            String courseFile = directory.getCanonicalPath(); // Tomcat环境中运行,会获取Tomcat安装目录的bin目录,不推荐使用
            System.out.println("第二种:获取项目路径");
            System.out.println(courseFile);
    
    
            /**
             * 第三种: /D:/Work/IdeaProjects/HelloVelocity/target/classes/
             */
            String path = ControllerUtils.class.getClassLoader().getResource("").getPath();
            System.out.println("第三种:");
            System.out.println(path);
    
    
            /**
             * 第四种: D:WorkIdeaProjectsHelloVelocity
             * 结果: C:Documents and SettingsAdministratorworkspaceprojectName
             * 获取当前工程路径
             */
            System.out.println("第四种:");
            System.out.println(System.getProperty("user.dir"));
    
    
            /**
             * 第五种:  获取所有的类路径 包括jar包的路径
             */
            System.out.println(System.getProperty("java.class.path"));

  • 相关阅读:
    你自己不优秀,就算认识再多优秀人又有何用
    史玉柱和他老同学的一段故事
    哪有雪中送碳,都是锦上添花
    围城之困
    心已死,梦前行
    一位销售高手逼单经历!
    Python--函数return多个值
    Python--内置函数
    Python--小程序
    Python--递归
  • 原文地址:https://www.cnblogs.com/zhang-cb/p/10608565.html
Copyright © 2011-2022 走看看