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"));

  • 相关阅读:
    面试题:JS中map的陷阱
    C#中正则表达式进行忽略大小写的字符串替换
    C#窗体钉在桌面、置底、嵌入桌面的办法
    创建C#串口通信程序详解
    为类和函数代码自动添加版权注释信息
    C# 如何编辑文件的摘要信息
    C# GDI在控件上绘图
    泛型Dictionary的用法详解
    Winform 导出成Excel打印代码
    C#反射深入学习
  • 原文地址:https://www.cnblogs.com/zhang-cb/p/10608565.html
Copyright © 2011-2022 走看看