Create a jar package in Java

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

 

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.
The configuration (manifest) file name and 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! ");
}
}
I willJavaSave the file to drive C and directory. OK. Next,

In the previous command prompt (jump to the C drive prompt), we enter javac helloworld.JavaAnd then enter: jar CVF hello. Jar helloworld. Class. Press enter and go to your drive C to see what is missing. 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 (run)

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.JavaPut it here and create a web. xml file at the same level as the classes directory. 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 --- 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.

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