Package the class file in Java into. Jar (jar command details)

Source: Internet
Author: User

 

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 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 // en"
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
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
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
Existing manifest file.

5. Update the JAR File

 

Jar UF jar-file input-file (s)
U --- want to update an existing JAR file.

 

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.