About the war package Jar bundle ear Package and packaging method

Source: Internet
Author: User
Tags pack rar

War package: After a Web application is done, it is usually a Web site that is packed into a container

Jar packages: Commonly used as a generic class to be referenced at development time, packed for easy storage management.

Ear Packs: Enterprise applications are usually EJB-typed ear packs

Various packs of the play:

Turn:

Brother, are you crazy about Java, or for your own livelihood, welcome you into the wonderful Java world, welcome. Maybe you just said to everyone: Hello world. Or ... ok. That's enough. So let's get started, the journey to the magical World:

Have you ever heard of a jar file? or unfamiliar. OK, that's okay, this is our first stop: Pack and release.

Why is this thing, first of all, this is the full name of the jar: JavaTM Archive (jar) file, yes, is the Java archive file. This is a bit like a zip file, think about what it is used for, compression. Right is to compress, to put our original scattered things to a bit, re-organized, all these purposes only one: convenient. Well, don't worry about how he compresses, we focus on what we want to compress (input) and what we compress (output), and then publish it (deploy).

So our input (what to compress) is mainly class file, and there are auxiliary resources (which may have pictures, JSP files, HTML files, etc.). Jar technology already exists in the jdk1.1 version and is enhanced in 1.2. Now let's talk about the benefits of the jar, which is the official description: Secure, fast download, compress, Hunt bag, version package, portable.

Having said so much, we are now beginning to implement.

Open a command prompt (Win2000 or execute the cmd command in the running basket, Win98 as a DOS prompt), enter Jar–help, and then return (if you have a jdk1.1 or above on your disk), see what:

Usage: jar {ctxu}[vfm0mi] [jar-file] [manifest-file] [-c directory] filename ...

Options:

-C Create a new archive
-t lists list of archived content
-X expand named (or all) files in the archive
-U Update Existing archive
-V build verbose output to standard output
-F Specify archive file name
-M contains marked information from the indicated file
-0 storage mode only; not in zip compression format
-M does not produce a manifest for all items (manifest) file
-I generates index information for the specified jar file
-C changes to the specified directory and contains the following files:

If a file name is a directory, it will be recursively processed.

The list (manifest) filename and archive file name) is specified in the same order as the ' m ' and ' F ' flags.

Example 1: Archive two class files into an archive file called ' Classes.jar ':
Jar CVF Classes.jar Foo.class bar.class

Example 2: Use an existing manifest (manifest) file ' mymanifest ' to archive all the files in the foo/directory to an archive file named ' Classes.jar ':
Jar CVFM Classes.jar mymanifest-c foo/.

A small example to try:
We have only one HelloWorld, as follows:

public class helloworld{
public static void Main (string[] args) {
System.out.println ("Hi, Hello world!");
}
}


I'm going to save this Java file in the C-disk directory, OK, Next,

At the command prompt that you opened earlier (jump to the C-drive prompt), we enter Javac Helloworld.java, and then continue typing: jar CVF hello.jar Helloworld.class, go back to your C disk and see what you have, yes. Hello.jar.

The basic steps we all know now, you can try it yourself. As the parameters behind the jar differ, the results change.

And then we'll see how to run our Jar pack.

Before you get to the point, you have to open the jar bag we just made to see what's more, Meta-inf directory. And look at what's inside, and a MANIFEST.MF file is not. Open it with a text editor (I'm UltraEdit here) to see:
manifest-version:1.0
created-by:1.4.2 (Sun Microsystems Inc.)

That's it. Here we modify it, add a sentence: Main-class:helloworld (in the third line). This is the class we wrote before, which is our entry class. Also namely,
manifest-version:1.0
created-by:1.4.2 (Sun Microsystems Inc.)
Main-class:helloworld

Next, we execute at a command prompt:
Jar UMF MANIFEST. MF App.jar

So we used our own MANIFEST.MF file to update the original default. You might as well go in and see if you have added a main-class:helloworld to this sentence.

Ok, this is the last step, to verify everything we do, at the command prompt, enter:
Java-jar Hello.jar (Executive)

What's happened, ――hi, Hello world!

Let's take a look at the jar file published in Tomcat, note: In Tomcat we can no longer use the jar in this format, but the war format, which is dedicated to Web applications, in fact, the entire process is basically the same as the jar is:

First prepare the resources we want to package.

Find the WebApps directory where Tomcat resides, go into it, create a new folder, name Hello, go into the new Web-inf folder, and go into the new Classes folder, and we'll also have our only servlet, Helloworld.java put it here and set up a file Web.xml with the classes directory sibling. Ok, we have initially established a simple Web application.

This is Helloworld.java:

Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void Doget (HttpServletRequest req, httpservletresponse Res)
Throws Servletexception, IOException {
Res.setcontenttype ("text/html");
PrintWriter out = Res.getwriter ();
Out.println ("<HTML>");
Out.println ("Out.println ("<BODY>");
Out.println ("Hello, world!");
Out.println ("</BODY></HTML>");
}
}//end here!


Compile on it. Here is Web.xml:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE Web-app Public
'-//sun Microsystems, INC.//DTD Web application 2.3//en '
' Http://java.sun.com/j2ee/dtds/web-app_2_3.dtd ' >
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>


Start compression to form a war file:

At the command prompt, go to the previously created Hello directory, execute jar CVF Hello.war *, and we get Hello.war. Copy it to the WebApps directory, OK, look at the final step, open the Server.xml in Tomcat's directory conf, and add:

<context path= "/hello" docbase= "Hello.war" debug= "0"
Reloadable= "true"/>


Done. Run it, start Tomcat, and then enter Http://localhost:8080/hello/HelloWorld in the browser.

Finally, if you want to use ant to do the above packing activities, let me tell you:

For jars. In the Build.xml,

<target name= "Jar" >
<jar destfile= "${app_home}/hello.jar" >
<fileset dir= "${dest}" includes= "* *"/>
<!--fileset dir= "${dest}" includes= "**/action.properties"/-->
</jar>
</target>


For war,

<war warfile= "Hello.war" webxml= "/web-inf/web.xml" >
<fileset dir= "html"/>
<lib dir= "lib/" >
<exclude name= "Oracle*.jar"/>
</lib>
<classes dir= "Build/servlets" >
<include name= "**/*.class"/>
</classes>
</war>


Well, that's all, I hope it helps you a little. :)

I uploaded two packs, Hello.jar and Hello.war. "Click to download"

"Click to download"

The first RAR file corresponds to the Hello.jar, the download will be changed to its name Hello.jar
The second RAR file corresponds to Hello.war, download and then change to Hello.war.
This is due to upload the jar format and war format files, you have to do as I said above:

Add:

############

Jar Basic Operation:

############

1. Create a JAR file

Jar CF Jar-file Input-file (s)
c---Want to Create a JAR file.
f---want the output to a file rather than to stdout.
EG:1) jar CF Myjar.jar Query_maintain_insert.htm
2) jar CVF Myjar.jar query_maintain_insert.htm
V---produces verbose (detailed) output.
3) Jar CVF Myjar.jar query_maintain_insert.htm mydirectory
4) Jar cv0f Myjar.jar query_maintain_insert.htm mydirectory
0---don ' t want the JAR file to be compressed.
5) Jar CMF MANIFEST. MF Myjar.jar Yahh.txt
M---Used to include manifest information. Existing manifest file.
6) Jar CMf MANIFEST. MF Myjar.jar Yahh.txt
M---The default manifest file should not is produced.
7) jar CVF Myjar.jar *
*---Create all contents into current directory.


2. View Jar file

Jar TF Jar-file

T---Want to view the Table of contents of the JAR file.

eg:1) jar Vft Yahh.jar

V---produces verbose (detailed) output.

3. Extract jar file

Jar XF Jar-file [Archived-file (s)]

X---Want to extract files from the JAR archive.

eg:1) Jar XF Yahh.jar Yahh.txt (extract files only yahh.txt)

2 jar XF Yahh.jar alex/yahhalex.txt (extract directory only alex file yahhalex.txt)

3 Jar XF Yahh.jar (extract all files or directories in the jar package)

4. Modify the manifest file

Jar CMF manifest-addition jar-file input-file (s)

M---Used to include manifest information. Existing manifest file.

5. Update jar file

Jar UF Jar-file input-file (s)

U---Want to update an existing JAR file.
References: http://www.blog.sh/user3/janson1983/archives/2006/47519.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.