About the difference between jar package and war package

For Java development, there are many contacts between jar packages and war packages. It is necessary to have an in-depth understanding of them. The summary is summarized as follows:

1. Introduction of jar package

JAR (Java Archive, Java Archive File) is a platform-independent file format that allows many files to be combined into a 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 for deploying and packaging libraries , components, and plug-ins , and can be used directly by tools like compilers and JVMs. Include special files in the JAR, such as manifests and deployment descriptors, to instruct the tool how to handle specific JARs.

In simple terms, jar packages are some classes that others have already written, and then package these classes. These jar packages can be introduced into your project, you can directly use the classes and attributes in these jar packages, these jar packages are generally placed in the lib directory.

2. Introduction of war package

War is a web module that can be run directly. It is usually used for websites and packaged into containers .

For Tomcat , place the war package in its \ webapps \ directory , and then start Tomcat, this package will be automatically decompressed, which is equivalent to release.

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 contains html and jsp files, or contains a directory of these two files, in addition to the WEB-INF directory. Usually contains a web.xml file and a classes directory in the WEB-INF directory, web.xml is the configuration file for this application, and the classes directory contains the compiled servlet class 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.

In simple terms, the war package is a package of JavaWeb programs . The war package includes the class files compiled by 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 everything in the project.

3. Difference: ( WAR file represents a web application, and JAR is a class archive file. )

If there are many directories and files in a web application, it is not very convenient to deploy the web application to another machine. At this time, you can package the web application 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 (excluding the root directory of the Web application hierarchy) and specify a .war extension.

It should be noted that although the file formats of WAR files and JAR files are the same, and are created using the jar command, as far as their applications are concerned, there is a fundamental difference between WAR files and JAR files. The purpose of the JAR file is to encapsulate the class and related resources into a compressed archive file , and for the WAR file, a WAR file represents a web application , which can contain Servlet, HTML pages, Java classes, image files, And other resources that make up a web application, not just archive files of a class.

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 contents of the Web application, update the Servlet class file, and after each change, re-establishing the WAR file will be a waste of time. In the product release phase, the use of WAR files is more appropriate , because at this time, almost no changes are required.

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 you can package it.

4. Deploy war package to Tomcat

1). In my work here, I usually develop a war package for testing. For example, the test now gets a war package named test.war.

2). Open the installation path of Tomcat, assuming it is "D: \ Tomcat \ apache-tomcat-7.0.68", then go to the webapps folder and put test.war in the webapps folder.

3). Start Tomcat.

If you do not need to change the configuration file: this step is all you need.

Enter "http: localhost: tomcat_port / test" in the browser to open the index.jsp page of the test project (port is its own port number). If the test project does not have an index.jsp page, you need to open other corresponding pages.

If you need to change the configuration file:

4). Close Tomcat.

5). Delete the test.war file (if you delete the war package when tomcat is started, the decompressed folder will also be deleted, so you need to stop tomcat after decompression, and then delete the war package. Start. At this time, the project folder will be considered not to be extracted from the war.).

6). Since Tomcat has just been started, Tomcat will automatically unzip test.war into the test folder. So we can see the test folder under webapps. Open the test folder to change the configuration file.

7). After updating the configuration, start Tomcat.

8). Open the browser.

 

Reference from: https://www.jianshu.com/p/3b5c45e8e5bd

 

 

 

Published 162 original articles · won 30 · 90,000 views +

Guess you like

Origin blog.csdn.net/ScorpC/article/details/102637881