Java compressed file into JAR package details

Source: Internet
Author: User

Open the command prompt (win2000 or run the cmd command in the running basket, win98 is the DOS prompt), enter jar-help, then press enter (if you already have JDK or a later version on your disk) to see what:
Usage: jar {ctxu} [vfm0Mi] [jar-file] [manifest-file] [-C Directory] File Name...

-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.
The name of the configuration (manifest) file and the archive file 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! ");
I saved this 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

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.

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 appears,-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.

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:
Reloadable = "true"/>
Success! Run it, start tomcat, and enter http: // localhost: 8080/hello/HelloWorld in the browser. Is there any?


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 --- dont 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.

--> Generate exe: <--

First: In jbuilder:
First, make sure that the Run menu --> Run Project can Run smoothly.
Then the Wizards menu --> Native Executable Builder
Select Compress the contents of the archive (if a jar file is generated)
Next --> select Always include all classes and resources and Next --> Next
Select Windows GUI "exe" (if an EXE file is generated) --> Finish
In the project file list, right-click Native Executable --> Make.

Second: generate a jar file in cmd
The content of abc.txt is as follows:
Manifest-Version: 1.0
Main-Class: main-class-name (Car return)
Under cmd:
Javac *. java
Jar cvfm abc. jar abc.txt *. class

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: 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.