1,默認eclipse有自己的classpath的路徑並不是環境變量中配置的classpah.
2,eclipse的classpath每個項目不同,一般是在工作區的當前項目的class下。
2.1,可以通過下面代碼查看:
System.out.println(System.getProperty("java.class.path"));
2.2也可以通過eclipse菜單查看:
Run→Run Configures...在classpath選項卡下就會發現,Eclipse默認是沒有導入系統的CLASSPATH路徑的
3,可以通過eclipse添加自己指定的classpath路徑:
1,在工程上右擊→Properties-java build path,在Libraries選項卡下,Add External Class Folder...,選擇自己需要導入的類文件夾
2,完成之后你會在工程下面看到Referenced Libraries
4,修改classpah的幾種方式:
1,當然就是直接在環境變量設置 2,在dos中使用set classpath=路徑
3,在dos使用java -cp 路徑
(cp是classpath的縮寫,dos環境設置的只是臨時的,窗口關閉也就失效了)
3,在代碼中設置System.setProperty(“java.library.path”,“classpath路徑”)
但是在代碼修改有個問題:
那就是classpath對應的是系統類加載器(AppClassLoader),那就是在代碼運行前系統類加載器已經實例化,也就是classpaht已經被確定了。
那么指定的路徑可能並不會起作用,路徑下的類會出現找不到的異常。
雖然修改了classpath的值,但是類加載器已經啟動了,再修改其classpath的值已經對那個已經啟動的類加載器或者說是jvm不起作用了。
google關鍵詞:“setting the classpath at runtime” 在此我貼出一些解釋 1)The classloader represents (part of) a namespace, and two otherwise identical classes loaded by different classloaders are not “equal”. Which means there are a few dangers lurking in classloading, notably singletons suddenly not being so single anymore as well as casts failing unexpectedly. 2)Classloaders (should) work on a pattern of delegating to the “parent” loader before attempting anything themselves (see above). 3)Class loading and linking are two distinct steps (even though example implementations of a classloader such as may be found in blog posts/online Java articles, will combine the two into one for simplicity) Therefore you should not assume that if a parent loader has loaded a class it has also loaded all dependencies … 4)All this means there is a problem if class A loaded by loader A references a class B which neither loader A nor any of its parents can load: class A may load just fine in loader A but at the point of use it fails because loader A cannot fully resolve (link) it. 5)And you should make sure that your classloader loads classes in a synchronized manner otherwise the issues hinted at in step #1 can leap from duplicates due to classloaders to duplicates from multiple threads using the same classloader as well…
