Java advanced learning: jar package details

Source: Internet
Author: User

Source: pconline Author: fivesky

Have you heard of jar files? Or a stranger! Okay, it doesn't matter. This is our first stop: Package and release.

Why is there this? First, this is the full name of jar: JavaTM Archive (JAR) file. Yes, it is the java Archive file. This is a bit similar to a zip file. Think about what it is for, compress it !? That's right. We need to compress the original scattered items and re-organize them. There is only one purpose: convenience! Well, you don't have to worry about how it is compressed. Our focus is on what we want to compress (input), what we want to compress (output), and then publish (deploy) it ).

Then our input (what we want to compress) is mainly a class file and auxiliary resources (which may include images, jsp files, html files, and so on ). Jar technology already exists in jdk1.1, and is enhanced in 1.2. Next, let's talk about the benefits of jar. This is the official description: Secure, fast download, compression, hunting, versioning, and portable.

After talking about this, we will implement it now.

Open the command prompt (win2000 or run the cmd command in the running box, win98 is the DOS prompt), enter jar Chelp, and press enter (if jdk1.1 or a later version already exists on your disk ), what do you see:

Usage: jar {ctxu} [vfm0Mi] [jar-file] [manifest-file] [-C Directory] File Name...

Option:

-C. Create a new archive

-T list the archived content

-X expand the named (or all) files in the archive

-U: update an existing archive

-V generates detailed output to the standard output

-F specifies the archive file name

-M contains the marker information from the specified file.

-0 storage only; zip compression format not used

-M does not generate a manifest file for all items

-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 is recursively processed.

Both the configuration (manifest) file name and the archive file name must be specified, in the same order specified by the 'M' and 'F' signs.

Example 1: Archive two class files to an archive file named 'classes. jar:

Jar cvf classes. jar Foo. class Bar. class

Example 2: use an existing manifest file 'mymanifest 'to archive all files in the foo/directory to an archive file named 'classes. jar:

Jar cvfm classes. jar mymanifest-C foo /.

Here is a small example:

We only have one HelloWorld, as shown below:

Public class HelloWorld {
Public static void main (String [] args ){
System. out. println ("Hi, Hello World !");
}
}

  

Save the java file to drive C and directory. OK. Next,

In the previous command prompt (jump to the C drive prompt), we enter javac HelloWorld. java, and then enter: jar cvf hello. jar HelloWorld. class, press enter and go to your drive C to see what's more. That's right, hello. jar.

Now we all know the basic steps. You can try again by yourself, as the parameters behind the jar are different, what are the changes in the results.
Then let's see how to run our jar package.

Before entering the subject, you need to open the jar package we just made to see, what is more, META-INF directory? Let's take a look at what is in it. There is also a MANIFEST. MF file, isn't it? Use a text editor (UltraEdit here) to open it:

Manifest-Version: 1.0
Created-By: 1.4.2 (Sun Microsystems Inc .)

That's it. Here we modify it and add the following sentence: Main-Class: HelloWorld (in the third line ). This is the class we wrote earlier, that is, our entry class. That is,

Manifest-Version: 1.0
Created-By: 1.4.2 (Sun Microsystems Inc .)
Main-Class: HelloWorld

Next, run:

Jar umf MANIFEST. MF app. jar (it should be hello. jar)

In this way, we use our own MANIFEST. MF file to update the original default file. You may wish to go in and see if you have added the Main-Class: HelloWorld sentence. (Why didn't I try it out? Why does it prompt java. io. FileNotFoundException: MANIFEST. MF (the system cannot find the specified file ?)

OK, this is the last step to verify what we have done. Enter the following in the command prompt:

Java-jar hello. jar (executed)

What happened, Hi, Hello World!

Let's take a look at the jar file released in tomcat. Note: In tomcat, we can no longer use the jar format, but change the war format, which is specially used for web applications, in fact, the whole process is basically similar to jar:

First, prepare the resources we want to package.

Find the webapps directory that stores tomcat, go to it, create a new folder, name it hello, and then go to the new WEB-INF folder, and then go to the new classes folder, at this time we will also be our unique servlet, helloWorld. java is put here, and a file web is created at the same level as the classes directory. xml. OK. At present, 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 ("");
Out. println ("");
Out. println ("");
Out. println ("Hello, World! ");
Out. println ("");
}
} // End here!

Compile it. Below is web. xml:
  
<? Xml version = "1.0" encoding = "UTF-8"?>
  
<! DOCTYPE web-app PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 //" http://java.sun.com/dtd/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>

Enter the previously created hello directory in the command prompt and execute jar cvf hello. war * to get hello. war. Copy it to the webapps directory, OK, and look at the last step. Open server. xml in the conf directory of tomcat and add:

<Context path = "/hello" docBase = "hello. war" debug = "0" reloadable = "true"/>

Success! Run it, start tomcat, and enter http: // localhost: 8080/hello/HelloWorld in the browser. Is there any?

Finally, if you want to use ant to complete the above packaging activities, we will tell you:
For jar. In 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 will be helpful to you. :)

Supplement:

Jar basic operations:

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 (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 from an existing manifest file.
6) jar cMf MANIFEST. MF myjar. jar yahh.txt
M --- the default manifest file shocould not be 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 (detailed) output.

3. Extract jar files

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 only the file yahh.txt)

2) jar xf yahh. jar alex/yahhalex.txt(only extract the file yahhalex.txt under alex)

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.
Related Article

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.