jar package and war package

What is the jar package

JAR (Java Archive) is a platform-independent file format that allows many files to be combined into one compressed file. JavaSE programs can be packaged into Jar packages (J can actually be understood as Java).

The JAR file format is based on the popular ZIP file format. Unlike ZIP files, JAR files are not only used for compression and distribution, but also used to deploy and package libraries, components, and plug-ins, and can be used directly by tools such as compilers and JVMs. Include special files in the JAR, such as manifests and deployment descriptors, to instruct the tool how to handle a specific JAR.

Simply put, a jar package is some classes that others have already written, and then these classes are packaged. These jar packages can be introduced into your project, and the classes and attributes in these jar packages can be used directly. These jar packages are generally placed in the lib directory.

What is the war package

War is a web module that can be run directly. It is usually used on websites and deployed as a package to a container. For Tomcat, place the war package in its \webapps\ directory, and then start Tomcat, the package will be automatically decompressed, which is equivalent to publishing.

The war package is a web application format proposed by Sun. Similar to jar, it is a compressed package of many files. The files in the war package are organized according to a certain directory structure. According to the root directory containing html and jsp files, or the directory containing these two files, there is also a WEB-INF directory. Usually there is a web.xml file and a classes directory in the WEB-INF directory, web.xml is the configuration file of this application, and the classes directory contains compiled servlet classes and jsp, or other classes that the servlet depends on ( Such as JavaBean). Usually these dependent classes can also be packaged into jar packages and placed in the lib directory under WEB-INF.

To put it simply, the war package is a package created by the JavaWeb program. The war package includes class files compiled from the written code, dependent packages, configuration files, and all website pages, including html, jsp, and so on. A war package can be understood as a web project, which contains all the things of the project.

The difference between jar package and war package

The WAR file represents a Web application, and the JAR is the archive file of the class.

If a web application has a lot of directories and files, it is not very convenient to deploy the web application to another machine. At this time, the web application can be packaged into a web archive (WAR) file. This process It is similar to the process of packaging Java class files into JAR files. Using WAR files, Servlet class files and related resources can be released together. In this process, the Web application is not deployed according to the directory hierarchy, but the WAR file is used as the deployment unit.

A WAR file is a web application. To create a WAR file is to compress the entire web application (not including the root directory of the web application hierarchy) and specify a .war extension.

It should be noted that although the file format of the WAR file and the JAR file are the same, and both are created using the jar command, there is a fundamental difference between the WAR file and the JAR file in terms of its application. The purpose of JAR files is to encapsulate classes and related resources into compressed archive files. For WAR files, a WAR file represents a Web application, which can include Servlets, HTML pages, Java classes, and image files. And other resources that make up a web application, not just archive files of classes.

So when should I use WAR files? It is not suitable to use WAR files in the development stage, because in the development stage, it is often necessary to add or delete the content of the Web application and update the Servlet class files. After each change, it will be a waste of time to re-create the WAR file. In the product release phase, it is more appropriate to use WAR files, because at this time, there is almost no need to make any changes.

In the development stage, we usually put the Servlet source file in the src subdirectory of the Web application directory to distinguish it from the Web resource file. When creating a WAR file, you only need to remove the src directory from the web application directory, and it can be packaged.

Guess you like

Origin blog.csdn.net/dingmengwei/article/details/112391536