java获取项目classPath路径

时间:2022-03-20 15:21:08


有时候我们会需要获取java工程中的classPath路径,以下方法可以实现:


Thread.currentThread().getContextClassLoader().getResource("/").getPath()

如果牵涉到跨操作系统,还要进行路径编码:

 URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource("/").getPath() + CommonsConstans.REDIS_PROPERTIES, "UTF-8");