1. 程式人生 > >eclipse工程中的.classpath檔案

eclipse工程中的.classpath檔案

java 編譯檔案

<?xml version="1.0" encoding="UTF-8"?>

<classpath>
<classpathentry kind="src" path="src/java"/> --指代原始碼所在的路徑
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>--指代專案的執行環境(jar) <!-- JDK編譯時容器,建立好java專案後自動產生的 -->
<classpathentry combineaccessrules="false" kind="src" path="/jade-component"/> --關聯專案
<classpathentry combineaccessrules="false" kind="src" path="/jade-core"/>
<classpathentry combineaccessrules="false" kind="src" path="/jade-hr-component"/>
<classpathentry combineaccessrules="false" kind="src" path="/jade-hr-res"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/jade-libs"/>--指代建立的user-library執行環境(jar)
<classpathentry combineaccessrules="false" kind="src" path="/jade-hr-orghc"/>
<classpathentry combineaccessrules="false" kind="src" path="/jade-hr-worktop"/>
<classpathentry kind="src" path="/wage-business"/>
<classpathentry kind="src" path="/wage-cal-engine"/>
<classpathentry kind="src" path="/gpos-wagebusiness"/>
<classpathentry kind="output" path="bin"/>--指代java的編譯成class檔案存放位置(輸出位置)

</classpath>

從資料上我們容易看出,上面描述了工程的依賴檔案:

  1. 原始檔的具體位置(kind="src")--指帶原始檔所在的路徑,如本專案src/java下或其它關聯專案
  2. 執行的系統環境(kind="con")--專案執行的底層支援(執行環境),如,關聯jar包,編譯環境
  3. 工程的library的具體位置資訊(kind="lib")
  4. 在每個lib的xml子節點中,有關於它的其它配置資訊(例如我配置的那個"javadoc_location")
  5. 專案的輸出目錄(kind="output")

1 前言

在使用eclipse或者myeclipse進行java專案開發的時候,每個project(工程)下面都會有一個.classpath檔案,那麼這個檔案究竟有什麼作用?

2 作用

.classpath檔案用於記錄專案編譯環境的所有資訊,包括:原始檔路徑編譯後class檔案存放路徑依賴的jar包路徑執行的容器資訊依賴的外部project等資訊。如果把該檔案刪除,則eclipse不能講該工程識別為一個正常的java工程,僅僅當做普通的資料夾而導致不能正常執行。

3 .classpath內容

.classpath檔案其實也是一個xml格式的內容檔案,其具體內容大致如下:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path
="src"/>
<classpathentry kind="src" path="resource"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7"> <attributes> <attribute name="owner.project.facets" value="java"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/學習 8080"> <attributes> <attribute name="owner.project.facets" value="jst.web"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/> <classpathentry kind="output" path="WebContent/WEB-INF/classes"/> </classpath>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

①以”classpath”為根節點,每個“classpathentry”節點代表一個說明資訊。 
②每個“classpathentry”以“kind”屬性指明型別,“path”指明路徑。 
③以上檔案的所有內容,都是依賴專案中的“Java Build Path”內容改變而改變的,即對“Java Build Path”的所有操作都會反應到檔案內容中。 
這裡寫圖片描述 
現在來具體分析檔案內容的每個節點含義

3.1 kind="src"

src:即source 原始檔,代表的是一個原始檔,path=”src”是一個相對路徑,相對.classpath檔案本身,即path=”src”表示資料夾src與.classpath在同一個目錄,且代表原始檔

<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="resource"/>
  • 1
  • 2

kind=”src”的操作對應於“Java Build Path”的“Source”tab頁 

建立原始碼存放檔案(並不是簡簡單單的新建個資料夾,這個會寫入classpath檔案中)

這裡寫圖片描述 
如下圖,增加一個資料夾bin作為原始檔,則.classpath檔案增加內容 
這裡寫圖片描述 
內容增加如下:
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="bin"/>
<classpathentry kind="src" path="resource"/>
  • 1
  • 2
  • 3

當然,做相應修改或刪除,同樣.classpath檔案內容會相應改變。 
另外,當指定屬性combineaccessrules=”false”是則代表引入外部project,具體如下

<classpathentry combineaccessrules="false" kind="src" path="/mybatis"/>
  • 1

對應頁面tab,其中path=”/mybatis”,是相對應workspace下的 
絕對路徑。 
這裡寫圖片描述

3.2 kind="output"

output用於指定java原始檔編譯後的class檔案存放路徑,格式如下

<classpathentry kind="output" path="WebContent/WEB-INF/classes"/>
  • 1

path:代表存放class檔案路徑,同樣是相對.classpath檔案的路徑,找到“WebContent/WEB-INF/classes”,可以看到class檔案的存放 
這裡寫圖片描述

假設修改輸出檔案路徑,則相應內容也會改變 
改為:spring-mybatis\WebContent\WEB-INF\newclasses 
這裡寫圖片描述 
則.classpath檔案內容修改為:

<classpathentry kind="output" path="WebContent/WEB-INF/newclasses"/>
  • 1

實際存放class檔案的路徑也會改變 
這裡寫圖片描述

3.3 kind="con"

con即是container,就是程式執行的容器,或者就說是執行環境也OK,它實際上是在Myeclipse最初的時候要配置installed JREs中指定(一般情況下我們指定的是JDK),但是這裡實際使用的是JDK下的JRE中的jar包,就是JDK_HOME/jre/lib就是對應的這條語句。具體內容如下

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7">
        <attributes>
            <attribute name="owner.project.facets" value="java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/學習 8080">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

對應操作tab如下 
這裡寫圖片描述

3.3.1 JRE配置

path=”XXX”用於指定jre容器的使用情況,可選擇jre的不同來源,不同來源path的實際值會有些變化 
這裡寫圖片描述 
①選擇”Workspace default JRE(jdk1.7)”,則值為

path="org.eclipse.jdt.launching.JRE_CONTAINER"
  • 1

②選擇“Alternate JRE”,則值為

path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7"
  • 1

其中最後麼的jdk1.7是我自定義的JRE名稱 
③選擇”Execution environment”,即選擇eclipse自帶的內建jre,值為

path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"
  • 1

其中最後麼的JavaSE-1.7是系統內建的JRE名稱 
另外屬性值name=”owner.project.facets”

<attribute name="owner.project.facets" value="java"/>
  • 1

應該是指定JRE容器的具體使用的模板,預設使用”java”,具體的沒有探究。

3.3.2 Server Runtime配置

同理,其他容器配置同上,如對Server Runtime 容器的配置,下面配置了tomcat容器執行環境

<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/學習 8080"/>
  • 1

3.3.3 Web App Libraries配置

<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
  • 1

3.3.4 User Library配置

<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/tomcat7"/>
  • 1

3.4 kind="lib"

kind="lib"用於指定project依賴的Referenced Libraries,如圖增加了一個jar包 
這裡寫圖片描述 
則相應.classpath檔案內容增加

<classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-dbcp-1.2.1.jar"/>一般大專案不會這樣做,而是將一些jar包單獨放置在一個專案中,又其它專案來引用此專案即可,如下配置<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/jade-libs"/>--指代建立的user-library執行環境(jar)
  • 1

其中path指定了依賴的jar的相對路徑。

3.5 順序

.classpath檔案中各節點的順序是通過tab-Order and Export 來控制的,不同的順序可能會引起載入class檔案問題,一般是原始碼放在最前面。 
這裡寫圖片描述