About the war package Jar packet Ear package and how to package it
War package: is a Web application, usually a Web site into a package deployed into a container
Jar Package: Typically a generic class to be referenced at development time, and a package for easy storage management.
Ear Package: Enterprise applications are typically EJB-made ear packs
Various packages of play:
Turn:
Brother, fascinated by Java, or for your own livelihood, no matter how welcome you into the wonderful Java world, welcome! Maybe you just said to everyone: Hello world! Or ... ok! That's enough. So let's start with the journey to the magical World:
Have you ever heard of jar files? or unfamiliar! OK, that's okay, this is our first stop: packing and publishing.
Why do you have this thing, first of all, this is the full name of the jar: JavaTM Archive (jar) file, yes, is the Java archive file. It's a bit like a zip file, think about what it's used for, compression!? Right is to compress, put our original scattered things to a bit, reorganize, all these purposes only one: convenient! Well, don't worry about how he compresses it, our focus is on what we want to compress (input), and what it compresses (the output), and then publish (deploy) it.
Our input (something to compress) is primarily a class file, as well as auxiliary resources (which may have images, JSP files, HTML files, etc.). Jar technology already exists in the jdk1.1 version and has been enhanced in 1.2. Let's talk about the benefits of jar, which is the official description: Secure, fast download, compress, Hunt package, version pack, portable.
Having said so much, we are now beginning to implement.
Open a command prompt (Win2000 or execute the cmd command in the Run basket, Win98 to the DOS prompt), enter Jar–help, and then return (if you already have jdk1.1 or above on the disk) and see what:
Usage: jar {ctxu}[vfm0mi] [jar-file] [manifest-file] [-C directory] File name ...
Options:
-C Create a new archive
-t lists the list of archived content
-X expands the named (or all) file in the archive
-U updates an existing archive
-V generates verbose output on standard output
-f Specifies the archive file name
-M contains marked information from the indicated file
-0 storage mode; Zip compression format not used
-M does not produce a list of 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 processed recursively.
The manifest (both the Manifest) file name and the archive file name need to be specified, in the same order specified by the ' m ' and ' F ' flags.
Example 1: Archive two class files into an archive file named ' Classes.jar ':
Jar CVF Classes.jar Foo.class bar.class
Example 2: Archive all files under the foo/directory to an archive file named ' Classes.jar ' with an existing manifest (manifest) file ' mymanifest ':
Jar CVFM Classes.jar mymanifest-c foo/.
Let's try this for a little example:
We have only one HelloWorld, as follows:
public class helloworld{
public static void Main (string[] args) {
System.out.println ("Hi, Hello world!");
}
}
I will save this Java file to the C drive and directory, OK, Next,
At a previously opened command prompt (jump to the C-drive prompt), we entered Javac Helloworld.java, and then continue to enter: Jar CVF Hello.jar Helloworld.class, go to your C-drive after the carriage return to see, more what, yes Hello.jar.
Basic steps we all know now, you can try it on your own. As the parameters behind the jar differ, the results change.
And then we look at how to run our jar package.
Before you get to the point, you have to open the jar package we just made and see, what's more, Meta-inf directory? And see what's inside, and a manifest.mf file, isn't it? 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 row). This is the class we wrote earlier, which is our entry class. That is,
manifest-version:1.0
created-by:1.4.2 (Sun Microsystems Inc.)
Main-class:helloworld
Next, we execute at the command prompt:
Jar UMF MANIFEST. MF App.jar
This allows us to use our own MANIFEST.MF file to update the original default. You might as well go inside and see if you've added Main-class:helloworld to this sentence.
Ok, this last step, to verify everything we do, at the command prompt, enter:
Java-jar Hello.jar (Executive)
What happened, ――hi, Hello world!.
Let's look at the jar file released in Tomcat, note: In Tomcat we can no longer use the format jar, and the war format, it is dedicated to the Web application, in fact, the whole process is basically similar to the jar:
Prepare the resources we want to pack first.
Find the WebApps directory where tomcat is stored, enter it, create a new folder, name it Hello, go in to create a new Web-inf folder, go inside the new classes folder, and we will also be our only servlet, Helloworld.java put it here, create a file, Web. XML, with the classes directory peer. Ok, we have initially built 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!
To compile 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 compressing to form a war file:
From the command prompt into the previously created Hello directory, execute jar CVF Hello.war *, we get hello.war. Copy it to the WebApps directory, OK, take a look at the last step, open the Tomcat directory conf in Server.xml, 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, do you have it?
Finally, if you want to use ant to complete the above packaging activities, let's 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 packages, Hello.jar and Hello.war. "Click to download"
"Click to download"
The first RAR file corresponds to Hello.jar, and after downloading it, it changes its name to Hello.jar
The second RAR file corresponds to Hello.war, and the download is changed to Hello.war.
This is due to not uploading jar format and war format files, you have to do as I said above to do:)
Add:
############
Basic jar 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 go 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 (verbose) 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 from an existing manifest file.
6) Jar CMf MANIFEST. MF Myjar.jar Yahh.txt
M---The default manifest file should not being produced.
7) jar CVF Myjar.jar *
*---Create all contents in current directory.
2. View the 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 (verbose) output.
3. Extracting the 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 Yahh.txt only)
2) Jar XF Yahh.jar Alex/yahhalex.txt (extract only the directory 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 from an existing manifest file.
5. Update the JAR file
Jar UF Jar-file input-file (s)
U---Want to update an existing JAR file.
War pack
1.war Package: Standard format: index.jsp (optional) + Meta-inf directory + Web-inf Directory
Index.jsp is dispensable.
The Meta-inf directory includes a MANIFEST.MF file that is automatically generated when packaged with the Jar tool.
The Web-inf directory includes the classes directory, the Lib directory, the Web. xml file, and the Web. xml file format as follows:
<?xml version= "1.0" encoding= "GB18030"?>
<web-app version= "2.4"
Xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Fight the War package method: JAR-CVF Xx.war contains files
Ear Package
2.ear Package: Standard format: War Pack +meta-inf Directory
A manifest.mf,application.xml,ibm-application-bnd.xmi,ibm-application-ext.xmi,was.policy is included in the Meta-inf directory.
Application.xml:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE application Public "-//sun Microsystems, Inc.//dtd j2eeapplication 1.3//en" "http://java.sun.com/dtd/ Application_1_3.dtd ">
<application id= "application_id" >
<display-name>AsiainfoBICASApplicationEAR</display-name>
<description>this is the Asiainfobicas application Server application.</description>
<module id= "Webmodule_1" >
<web>
<web-uri>example.war</web-uri>
<context-root>/abc</context-root>
</web>
</module>
</application>
IBM-APPLICATION-BND.XMI:
<?xml version= "1.0" encoding= "UTF-8"?>
<applicationbnd:applicationbinding xmi:version= "2.0" xmlns:xmi= "Http://www.omg.org/XMI" xmlns:applicationbnd= " Applicationbnd.xmi "xmlns:common=" Common.xmi "xmlns:application=" Application.xmi "xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "xmi:id=" Application_id_bnd ">
<appname xsi:nil= "true"/>
<application href= "meta-inf/application.xml#application_id"/>
</applicationbnd:ApplicationBinding>
IBM-APPLICATION-EXT.XMI:
<?xml version= "1.0" encoding= "UTF-8"?>
<applicationext:applicationextension xmi:version= "2.0" xmlns:xmi= "Http://www.omg.org/XMI" Xmlns:applicationext = "Applicationext.xmi" xmlns:application= "Application.xmi" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xmi:id= "Application_id_ext" sharedsessioncontext= "false" >
<application href= "meta-inf/application.xml#application_id"/>
</applicationext:ApplicationExtension>
Was.policy:
WebSphere application Server Security Policy for the Default application
Required for Snoop Servlet's call to get the User Name from Getuserprincipal ()
Packing method: JAR-CVF XX.earXX.war Meta-inf
About the war package Jar packet Ear package and how to package it