跳到主要內容

發表文章

目前顯示的是 7月, 2010的文章

Java 執行檔(exe)路徑問題

要讀取應用程式下相對目錄的檔案,要瞭解java File解析相對路徑的方式。 JavaDoc java.io.File中提到 A pathname, whether abstract or in string form, may be either absolute or relative . ...resolve relative pathnames against the current user directory. This directory is named by the system property user.dir , and is typically the directory in which the Java virtual machine was invoked. 路徑可以是「相對」或「絕對」,相對路徑依照目前使用者的所在目錄,也就是JM被啟動的目錄。 在eclipse中,啟動的目錄是project根目錄,例如: myProject下執行"run as Java Application" current user directory就是 [workspace目錄]\myProject 如果用JSmooth之類的custom launcher工具封裝成exe後,current user directory就是exe所存在的目錄。 建立成捷徑後,則current user directory會以捷徑內容中「開始位置」為準,所以如果「開始位置」與exe存放位置相同的時候,則與直接雙擊exe執行的current user directory相同。 但若以bat執行,則current use directory會以bat所在目錄為準。 例如一個bat檔如下 d:\filePath.exe pause 若filePath會建一個新檔在current user directory,若bat放在桌面,則該新檔會建立在桌面,而不是exe檔所在位置。 但透過JSmooth設定可以改變此情形。 勾選"Set the executable folder as current directory of application"之後,無論bat檔在什麼位置,都以exe檔的所在位