2010-10-01 98 views
0

我有一個應用程序有一些第三方庫需要一些配置文件存在。我把它們放在jar文件中,這樣它們就可以使用JWS進行部署。我知道你可以使用getResourceAsStream()從jar文件讀取文件,但第三方庫不這樣做。我試着用getResourceAsStream將它們從jar中讀出來,並將實際的文件寫入user.dir。該應用程序正常工作的方式。但是,似乎大多數用戶從桌面上的快捷方式啓動應用程序,因此他們的user.dir當然是桌面。他們寧願不使用配置文件亂扔桌面。在Java Web中讀取文件開始

有沒有更好的辦法可以處理呢?

回答

0

你實際上可以改變你的user.dir。起初我認爲它是「不可觸摸的」。

所以首先你需要使它「可觸摸」

我們調用使用靜態塊我們班以下MainWindow

static { 
    try { 
    Class clazz = ClassLoader.class; 
    Field field = clazz.getDeclaredField("sys_paths"); 
    boolean accessible = field.isAccessible(); 
    if (!accessible) { 
     field.setAccessible(true); 
    } 
    field.set(clazz, null); 
    try { 
     String currentDir = System.getProperty("java.library.path"); 
     //now remove the current directory from the library path. 
     String sansCurrentDir = removeCurrentDirectoryFromPath(currentDir); 
     System.setProperty("java.library.path", sansCurrentDir); 
    } 
    finally { 
     field.setAccessible(accessible); 
    } 
} catch (Exception e) { 
    //in our case we rethrow 
} 
} 


//and then later in our code in the same static block we can now create our root directory (ie, the user.dir) and execute 
System.setProperty("user.dir", rootDirectory);