2011-05-25 63 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; 
    }