找到eclipse儲存java工程使用的workspace檔案夾。
開啟一個java工程,這裡開啟的是Heima工程,會發現裡面有一些目錄和檔案。
其中有一個.classpath檔案。那麼.classpath是幹嘛用的呢。裡面寫的是什麼東西。
用計算本開啟來看:
看不懂是什麼意思。。網上找了一些資料:
<!-- 源碼目錄 -->
<classpathentry kind="src" path="src"/>
<!-- JDK運行時容器 -->
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<!-- 以下為類庫 path為你自訂的目錄 -->
<classpathentry kind="lib" path="lib/swing-layout-1.0.3.jar"/>
<!-- 編譯後輸出class 目錄 -->
<classpathentry kind="output" path="bin"/>
原來如此:src目錄是存放原始碼的目錄,而bin是存放class位元組碼檔案的目錄。
----------------------
我把.classpath檔案給修改一下:
把<classpathentry kind="output" path="bin"/> 中的path改為“bi”
修改.classpath檔案修改後重啟eclipse
運行:CopyText.java
運行後:錯誤: 找不到或無法載入主類 day18.CopyText
解決辦法:看完以下分析,只要把原來bin目錄下day18目錄拷貝到bi檔案夾下就可以了。
------------------
修改.classpath檔案之後,重啟eclipse,在工程的src下建立一個black包
會看到工程目錄下新多了一個檔案夾bi,而bi檔案夾下又出現一個black的檔案夾
black檔案夾下是空的。。
--------------------
現在在eclipse裡的Heima工程black包裡建立立一個Test.java
public class Test {public static void main(String[] args) {System.out.println("hello");}}
然後儲存,會發現black檔案夾下產生了Test.class檔案
---------------
在eclipse裡編譯運行Test.java程式輸出:
---------------
在black檔案夾下建立一個eula.txt檔案,使用類載入器載入
Test.class.getClassLoader().getResourceAsStream("black/eula.txt");InputStream is = Test.class.getResourceAsStream("eula.txt");FileWriter fw = new FileWriter("mm.txt");int ch = 0;while((ch=is.read())!=-1){fw.write(ch);fw.flush();}最後在Heima的工程下產生了一個mm.txt檔案。
因此可知IO流的相路徑是工程。
類載入器相對載入路徑是.classpath裡指定的存放class位元組碼檔案的目錄。
在myeclipse的.classpath中指定的存放class位元組碼檔案的目錄是WebRoot/WEB-INF/classes
<classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
所以在myeclipse中使用類載入器負載檔案的時候,也會相對去WebRoot/WEB-INF/classes目錄下載入。
-------------------在Eclipse裡設定Build Path
Window->Preferences->Java->Build Path
在eclipse裡設定存放原始碼的目錄和存放class位元組碼檔案的目錄。建立一個工程,那個工程就開始生效,並且那個工程的.classpath檔案也是對應的。
------------------------------
應用:採用設定檔加反射的方式建立HashSet的執行個體對象,觀察運行結果。
設定檔config.properties的內容是:
className=java.util.HashSet
1、使用傳統IO流讀取設定檔
import java.io.*;import java.util.*;public class PropsLoad {public static void main(String[] args) throws Exception {InputStream ips = new FileInputStream("config.properties");//IO流讀取設定檔Properties props = new Properties();props.load(ips);String className = props.getProperty("className");Collection collections = (Collection)Class.forName(className).newInstance();//反射ReflectPoint pt1 = new ReflectPoint(3,3);ReflectPoint pt2 = new ReflectPoint(5,5);ReflectPoint pt3 = new ReflectPoint(3,3);collections.add(pt1);collections.add(pt2);collections.add(pt3);collections.add(pt1);System.out.println(collections.size()); }}
在eclipse裡,如果把 config.properties檔案放在和PropsLoad.java同一個包裡的話或者src目錄下,運行:
Exception in thread "main" java.io.FileNotFoundException: config.properties (系統找不到指定的檔案。)
那麼怎麼放置才可以呢。
對於IO流來說,使用相對路徑,直接寫“config.properties”路徑的話,config.properties檔案要放在你的工程目錄下。
2、使用類載入器載入設定檔。
現在把config.properties檔案放在了工程目錄下
import java.io.*;import java.util.Collection;import java.util.Properties;public class ClassLoad {public static void main(String[] args) throws Exception {InputStream ips= ClassLoad.class.getClassLoader().getResourceAsStream("config.properties");//使用類載入器進行載入資源檔Properties props = new Properties();props.load(ips);ips.close();String className = props.getProperty("className");Collection collections = (Collection)Class.forName(className).newInstance();//Collection collections = new HashSet();ReflectPoint pt1 = new ReflectPoint(3,3);ReflectPoint pt2 = new ReflectPoint(5,5);ReflectPoint pt3 = new ReflectPoint(3,3);collections.add(pt1);collections.add(pt2);collections.add(pt3);collections.add(pt1);System.out.println(collections.size()); }}
運行結果: Exception in thread "main" java.lang.NullPointerException
出現null 指標異常,因為ips引用為null,說明類載入器沒有找到設定檔,提供的路徑不正確或者說是config.properties檔案存放的路徑不對。
----------------
那麼現在就要知道類載入器負載檔案預設相對於哪個目錄呢。
這時候就要看eclipse下,這個工程的.classpath檔案了。
看到這個.classpath檔案中 <classpathentry kind="output" path="bin"/>
就知道了,類載入器負載檔案預設相對於bin這個目錄。因此要把 config.properties檔案存放在bin目錄下。再次運行就OK了。
【類載入器去找class檔案存放的路徑(bin目錄下存放class檔案,bin就是.classpath檔案設定好的)】
------------------
另外Class類的API裡也提供了InputStream getResourceAsStream(String name)方法。
用某個類去載入,而不是類載入器,其實內部還是調用的getClassLoader
那麼就可以這樣寫來載入資源檔了:
InputStream ips= ClassLoad.class.getResourceAsStream("config.properties");
使用類載入有什麼好處呢。
這個Class類提供的這個getResourceAsStream方法很爽的,只需要把設定檔和來源程式放在同一包裡面就可以了。
如果在eclipse下開發,就把設定檔放在src目錄或者src的某個子目錄包下面,它會自動拷貝到bin目錄或者bin目錄與src目錄對應的子目錄下面去。
(bin目錄和src目錄是對應的)
------------
用某個類去載入,既可以用相對路徑,還可以用絕對路徑。只要打斜杠“/”就不是相對的了,而是相對於classpath的根(classpath的根就是bin,/就代表bin)。
ClassLoad.java是在cn.itcast.enhance包下面的來源程式。並且config.properties和ClassLoad.java在同一個包裡
這時候使用絕對路徑想要成功載入的話。就要從根“/”開始寫上完整的路徑:
InputStream ips= ClassLoad.class.getResourceAsStream("/cn/itcas/enhance/config.properties");
如果資源檔和我的包有關係,我就用相對的路徑。如果和我的包沒關係,就用絕對的路徑。
不管是相對還是絕對,內部調用的都是getClassLoader。