From the end customer's point of view, the JAR file is a kind of encapsulation. They do not need to know how many .class files there are in the jar file. The functions and functions of each file can also get the results they want. In addition to jar, there are war and ear for J2EE. See the difference in the table below:
JAR | WAR | EAR | |
English | Java Archive file | Web Archive file | Enterprise Archive file |
Contains content | Class and properties files are the smallest unit of file encapsulation; they include common libraries, resources, auxiliary files, etc. of Java classes. | Servlets, JSP pages, JSP tag libraries, JAR library files, HTML/XML documents and other public resource files, such as pictures, audio files, etc. | In addition to JAR and WAR, it also includes EJB components |
deployment file | application-client.xml | web.xml | application.xml |
container | application servers | servlet containers | EJB containers |
level | Small | middle | big |
(1)Packaging of EAR files
The EAR file includes the entire project, containing multiple EJB Modules (JAR files) and Web Modules (WAR files).
The EAR file can be generated using winrar in zip compression or using the jar command from the command line.
step:
1>. First package it into war and jar, write application.xml, and put it in the META-INF directory.
2>. Run jar cf your_application.ear your_war.war your_jar.jar META-INF/application.xml (assuming they are all in the current directory).
Of course, you can also use jar xf your_application.ear to decompress it.
application.xml is used to describe the war and jar included in your ear
Example of application.xml of petstore that comes with weblogic:
1>. To generate a war file, use the following command: jar -cvf web1.war *
2>. To check what files are in web1.war, you can use the command: jar -tf web1.war
3>. To decompress the web1.war file, you can use the command: jar -xvf web1.war
In addition, you can also use the winrar software to select the zip compression method and change the suffix of the compressed file to war to compress and generate a war file; you can also use the winrar software to forcibly open the war file or forcibly decompress the war file.
The difference between using the jar command and the winrar software is that the former will generate a META-INF folder containing the MANIFEST.MF file while compressing the file.
(3) When to use war or jar files
When your project is not fully completed, it is not suitable to use war files, because your classes will be frequently changed due to debugging and so on. It is very uncomfortable to delete and create war files back and forth. It is best that your project has been completed. If you don’t want to change it, then make a war package. At this time, a war file is equivalent to a web application; and a jar file encapsulates classes and some related resources into a package for easy reference in the program.
++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++
The functions, differences, and packaging methods of Java's packaged jar, war, and ear packages:
1. Function and difference
i. jar: Usually, common (JAVA) classes need to be referenced during development and packaged into packages for easy storage and management;
ii. war: After making a (web) application, usually a website, it is packaged and deployed into a container;
iii. ear: Enterprise-level applications. In fact, the EAR package only contains the WAR package and the configuration files of several enterprise-level projects. Generally, servers such as WebSphere will use the EAR package. Usually EJB is packaged as ear.
2. Packaging method
i. All packages are created using jar, but the extensions of the target files are different;
ii. You can also use Ant to install the build.
3. JET compiled into EXE
i. JET has to be bought with money, and it is said that JET cannot compile all Java programs into executable files, and the performance will be compromised. Therefore, using the method of making an executable JAR file package is the best choice, not to mention that it can maintain the cross-platform characteristics of Java.
Notice:
After exporting the web project as war, place it directly under webapps in the tomcat container, start the service, and then run the project. The war package will automatically decompress a folder with the same name.