java获取类加载路径和项目根路径的5种方法
2024-10-10 23:35:55
// 第一种:获取类加载的根路径 D:\IDEAWorkspace\hs-bluetooth-lock\src\applications\bluetooth-api\target\classes
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f); // 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录 D:\IDEAWorkspace\hs-bluetooth-lock\src\base\target\classes\com\hs\lock\bluetooth\base\utils
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println(f2); // 第二种:获取项目路径 D:\IDEAWorkspace\hs-bluetooth-lock
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile); // 第三种: file:/D:/IDEAWorkspace/hs-bluetooth-lock/src/applications/bluetooth-api/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");
System.out.println(xmlpath); // 第四种: 获取当前工程路径 D:\IDEAWorkspace\hs-bluetooth-lock
System.out.println(System.getProperty("user.dir")); // 第五种: 获取所有的类路径 包括jar包的路径
System.out.println(System.getProperty("java.class.path"));
最新文章
- div内容上下居中
- Python3基础 random 产生置顶区间的随机整数
- 【转】VB中应用DDE
- linux centos各种问题解决汇总
- MVC去掉传参时的验证:从客户端中检测到有潜在危险的Request.QueryString值
- WordPress RokNewsPager插件‘thumb.php’多个安全漏洞
- Mediator - 中介者模式
- jq判断元素是否显示
- php实现仿百度分页
- vue版 文件下载
- English Conversations You Can Download for Free (Spoken English MP3/Audio Files)
- C/S,B/S的应用的区别
- Python 基于python实现单例模式
- std::shared_ptr 和普通指针的转换
- webbrowser获取无ID无Name控件并模拟点击
- WPF 从文件加载字体
- UVALive 5058 Counting BST 数学
- windows mysql默认配置文件
- Transform动画初解 in Swift
- 关于node npm的一个解决方法
热门文章
- MyEclipse 新手使用教程---图文详解
- 用gogs轻松搭建个人的git服务器
- .NET快速信息化系统开发框架 V3.2->;Web版本新增“文件管理中心”集上传、下载、文件共享等一身,非常实用的功能
- Python3+Selenium2完整的自动化测试实现之旅(四):Selenium-webdriver操作浏览器、Cookie、鼠标键盘、警示框、设置等待时间、多窗口切换
- [Linux] memache打印所有的key
- [PHP] defunct僵尸进程
- jquery 实现省市二级联动,附带完整的省市json数据 (粘贴即用)
- 单元测试与Mockito
- 由AbstractQueuedSynchronizer和ReentrantLock来看模版方法模式
- Python re 模块