Java開(kāi)發(fā)中關(guān)于資源路徑獲取問(wèn)題

描述

在開(kāi)發(fā)中經(jīng)常會(huì)讀取配置文件,在Web開(kāi)發(fā)中大多數(shù)都是在項(xiàng)目路徑下。核心的API類(lèi)或者是Controller異或是jsp頁(yè)面等,基本都是基于web應(yīng)用的相對(duì)路徑,很少去操作絕對(duì)路徑,但是在客戶(hù)端、jar啟動(dòng)方式、exe方式情況下,獲取資源文件的路徑就會(huì)是一個(gè)相對(duì)不同的問(wèn)題。

最近公司有個(gè)開(kāi)發(fā)需求,非網(wǎng)絡(luò)的pc客戶(hù)端處理需求。很多操作都可以收集、編輯放到配置文件去批處理執(zhí)行,這時(shí)候遇到一個(gè)問(wèn)題,就是在打jar包的時(shí)候,發(fā)現(xiàn)有個(gè)詭異的區(qū)別。

代碼:

點(diǎn)擊查看代碼

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">- [JarPropertiesTest main = new JarPropertiesTest();
String root = main.getClass().getResource("/").getPath();//第二次嘗試獲取路徑方法
System.out.println(System.getProperty("user.dir"));
System.out.println("root:" + root);
System.out.println(main.getClass().getProtectionDomain().getCodeSource().getLocation()
.getFile());
String jarpath = main.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();//第一次獲取路徑方法
System.out.println(jarpath);
jarpath = jarpath.indexOf(".jar") > -1 ? root : jarpath;//第三次為了兼容幾種不同結(jié)果
// if (jarpath.indexOf(".jar") > -1) jarpath = jarpath.substring(0, jarpath.lastIndexOf("/") +
// 1);
jarpath = jarpath + "configs/config.ini";
Properties properties = new Properties();
try {
properties.load(new FileInputStream(jarpath));
System.out.println(properties.get("params"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} ]</pre>

情況描述:

打包方法:Eclipse自帶的Export和Ant

Eclipse中打包時(shí),下面代碼是生效的,可以直接拿到j(luò)ar包存放的路徑,然后configs目錄與jar文件同級(jí),則可以正常執(zhí)行

采用main.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()

用ant打包時(shí)候,采用main.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()方法得到的路徑則是jar路徑且?guī)ar文件名,是個(gè)全路徑,需要自己手工去掉多余內(nèi)容

<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Eclipse打包時(shí)main.getClass().getResource("/").getPath()得到是一個(gè)空字符串
ant打包時(shí)main.getClass().getResource("/").getPath()得到的是正確路徑
如下圖:</pre>

[圖片上傳失敗...(image-ea6718-1650024380678)]

[圖片上傳失敗...(image-d40806-1650024380678)]

<pre class="prettyprint hljs cmake" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">下面代碼用ant打包時(shí)候可以正常獲取到j(luò)ar的存放路徑,進(jìn)而可以構(gòu)建同級(jí)目錄configs下文件路徑
String filePath = System.getProperty("user.dir") + "/configs/config.ini";</pre>

總結(jié)

對(duì)于jar或者exe情況下自動(dòng)獲取相對(duì)路徑下的文件情況,既要考慮操作系統(tǒng)環(huán)境又要考慮打包方式,所以要對(duì)根路徑進(jìn)行適配,也就是

<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">String filePath = System.getProperty("user.dir") + "/configs/config.ini";
String root = main.getClass().getResource("/").getPath();
String jarpath = main.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();</pre>

都要獲取,并進(jìn)行判斷,最終得到準(zhǔn)確的根路徑。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容