1. 程式人生 > >java獲取專案classPath路徑

java獲取專案classPath路徑

有時候我們會需要獲取java工程中的classPath路徑,以下方法可以實現:

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

如果牽涉到跨作業系統,還要進行路徑編碼:
 URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource("/").getPath() + CommonsConstans.REDIS_PROPERTIES, "UTF-8");