2011-05-25 62 views
1

每當有人部署應用程序時,這個問題就是一個問題:在部署項目後,java在哪裏查找庫(jar和dll)?java中的庫查找路徑

最好的問候, 斯特凡

+1

更具體一點,請問:什麼是*項目*,什麼是*部署*,在您的問題的背景下! – 2011-05-25 14:17:14

+0

不,這個問題應該以通用的方式回答,這適用於99%的開發應用程序! – Stefan 2011-05-25 14:41:06

+0

更精確:*在J2EE上下文中* project *和* deploy *與在簡單的Java應用程序或applet中執行相同的操作不同。 * project *可能是一個IDE相關術語(如:eclipse項目)。 – 2011-05-25 14:44:28

回答

0

在其classpath

在應用程序服務器上,通常設置了很多路徑。通常,您可以檢查啓動日誌或登錄以下屬性的值,以確定其尋找:

System.getProperty("java.class.path") 
+0

..這是不適用於本地庫。 – 2011-05-25 14:19:31

0

http://en.wikipedia.org/wiki/Classpath_%28Java%29

**虛擬機搜索和加載類的順序如下:

bootstrap classes: the classes that are fundamental to the Java Platform (comprising the public classes of the Java Class Library, and the private classes that are necessary for this library to be functional). 
extension classes: packages that are in the extension directory of the JRE or JDK, jre/lib/ext/ 
user-defined packages and libraries 

**

+0

爲什麼?你想複製和粘貼從維基百科?我剛剛給你的源 – anfy2002us 2011-05-25 14:18:50

1

它看起來幾個不同的地方,如其他答案建議。您可以使用System.getProperty("java.library.path")System.getProperty("java.class.path")來查看實際路徑。

下面的代碼我也發現很有用。您可以使用它在運行時添加搜索到的庫路徑的路徑。

 

    /** 
    * Allows you to add a path to the library path during runtime 
    * @param dllLocation The path you would like to add 
    * @return True if the operation completed successfully, false otherwise 
    */ 
    public boolean addDllLocationToPath(final String dllLocation) 
    { 
     //our return value 
     boolean retVal = false; 
     try 
     { 
      System.setProperty("java.library.path", System.getProperty("java.library.path") + ";" + dllLocation); 
      //get the sys path field 
      Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); 
      fieldSysPath.setAccessible(true); 
      fieldSysPath.set(null, null); 
      retVal = true; 
     } 
     catch (Exception e) 
     { 
      System.err.println("Could not modify path"); 
     } 
     return retVal; 
    }