以最終客戶的角度來看,.jar檔案就是一種封裝,他們不需要知道.jar檔案中有多少個.class檔案,每個檔案中的功能與作用,同樣可以得到他們希望的結果。除jar以外對於J2EE來說還有war和ear。區別見下表:
| |
JAR |
WAR |
EAR |
| 英文 |
Java Archive file |
Web Archive file |
Enterprise Archive file |
| 包含內容 |
class、properties檔案,是檔案封裝的最小單元; |
Servlet、JSP頁面、JSP標記庫、JAR庫檔案、HTML/XML文檔和其他公用資源檔,如圖片、音頻檔案等; |
除了包含JAR、WAR以外,還包括EJB組件 |
| 部署檔案 |
application-client.xml |
web.xml |
application.xml
|
| 層級 |
小 |
中 |
大 |
經常在我們Java Web項目中看到jar/war/ear這幾種檔案類型,它們之間有何不同之處呢。我查了一些資料歸納整理如下:
jar/war /ear三種檔案,在檔案結構上,三者並沒有什麼不同,它們都採用zip或jar檔案檔案壓縮格式。但是它們的使用目的有所區別:
Jar檔案(副檔名為. Jar)包含Java類的普通庫、資源(resources)、輔助檔案 (auxiliary files)等
War檔案(副檔名為.War)包含全部Web應用程式。在這種情形下,一個Web應用程式被定義為單獨的 一組檔案、類和資源,使用者可以對jar檔案進行封裝,並把它作為小型服務程式(servlet)來訪問。
Ear檔案(副檔名為.Ear)包含全部公司專屬應用程式程式。在這種情形下,一個公司專屬應用程式程式被定義為多個jar 檔案、資源、類和Web應用程式的集合。
每一種檔案(.jar, .war, .ear)只能分別由應用伺服器(application servers)、小型服務程式容器(servlet containers)、EJB容器(EJB containers)等進行處理。
JAR WAR EAR
英文 Java Archive file ; Web Archive file Enterprise Archive file
jar:包含內容 class、properties檔案,是檔案封裝的最小單元; war:Servlet、JSP頁面、JSP標記庫、JAR庫檔案、HTML/XML文檔和其他公用資源檔,如圖片、音頻檔案等;ear:除了包含 JAR、WAR以外,還包括EJB組件
部署檔案 application-client.xml web.xml application.xml
層級 小 中 大
jar:封裝類 war:封裝web網站 ear:可以封裝ejb
JAR: Software developers generally use .jar files to distribute Java applications or libraries, in the form. of classes and associated metadata and resources (text, images, etc.) JAR files build on the ZIP file format.
WAR: In computing, a WAR file (which stands for "web application archive" ) is a JAR file used to distribute a collection of JavaServer Pages, servlets, Java classes, XML files, tag libraries and static Web pages (HTML and related files) that together constitute a Web application.
EAR: An Enterprise ARchive, or EAR, is a file format used by Java EE for packaging one or more modules into a single archive so that the deployment of the various modules onto an application server happens simultaneously and coherently. It also contains XML files called deployment descriptors which describe how to deploy the modules. Maven or Ant can be used to build EAR files.
RAR: A Resource Adapter is an archive file format defined in the J2EE Connector Architecture (JCA) specification. A Resource Adapter aRchive (RAR) file is the valid format for deployment of resource adapters on application servers. J2EE RAR files may also be called connectors.
其他概念:
EJB: Enterprise JavaBeans (EJB) is a managed, server-side component architecture for modular construction of enterprise applications. The EJB specification is one of several Java APIs in the Java EE. EJB is a server-side model that encapsulates the business logic of an application.
JavaBean: JavaBeans are reusable software components for Java that can be manipulated visually in a builder tool. Practically, they are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects. A JavaBean is a Java Object that is serializable, has a nullary constructor, and allows access to properties using getter and setter methods.
Metadata: Metadata is a means to describe the data files.It provides information about a certain item's content, such as: means of creation, purpose of the data, time and date of creation, creator or author of data, placement on a network (electronic form) where the data was created, what standards used (ISO9000), etc.