class files and jar files in Java

Source: Internet
Author: User

1. JAR File Package

The JAR file is Java Archive file, as implies, its application is related to Java, is a Java document format. The JAR file is very similar to a zip file--exactly, it is a zip file, so call it a file package. The only difference between a jar file and a ZIP file is that it contains a meta-inf/manifest in the contents of the jar file. MF file, this file is created automatically when the JAR file is generated. For example, if we have some files with the following directory structure:

==

'--Test

'--Test.class

Compress it into a zip file test.zip, the internal directory structure of this zip file is:

Test.zip

'--Test

'--Test.class

If we use the JDK jar command to call it a jar package Test.jar, the internal directory structure of this jar file is:

Test.jar

|--Meta-inf

| '--MANIFEST. Mf

'--Test

'--test.class

2. Create an executable JAR file package

Creating an executable jar file package to publish your program is the most typical use of jar packages.

A Java program is made up of several. class files. These. class files must be classified according to the package they belong to, and the root directory of all the packages used should be assigned to the-CP parameters of the CLASSPATH environment variable or Java command before running, and the runtime will go to the console and run with Java commands. If you need to directly double-click Run a batch file (. bat) or a Linux shell program that must be written to Windows. As a result, many people say that Java is a user-friendly programming language for developers.

In fact, if the developer can make an executable JAR file package to the user, then the user is convenient to use. When installing the JRE (Java Runtime Environment) under Windows, the installation file maps the. jar file to Javaw.exe open. Then, for an executable JAR package, the user can simply double-click on it to run the program, as easily as reading the. chm document (. CHM documents are opened by default by hh.exe). The key now, then, is how to create this executable JAR file package.

To create the executable jar package, you need to use the jar command with the CVFM parameter, as an example of the test directory above, the command is as follows:

Jar CVFM Test.jar MANIFEST.MF test

Here Test.jar and manifest.mf two files, respectively, corresponding to the parameters F and M, whose plays are in MANIFEST.MF. Because to create an executable jar package, it is not sufficient to specify a MANIFEST.MF file, because manifest is a feature of the jar package, both the executable jar package and the non-executable jar package contain manifest. The key is to execute the MANIFEST of the JAR file package, which contains the Main-class. This is written in MANIFEST in the following format:

Main-class: Executable main class full name (including package name)

For example, assuming that the test.class in the previous example belongs to the Test package and is an executable class (which defines the public static void Main (string[]) method, then this MANIFEST.MF can be edited as follows:

Main-class:test. Test < Enter >

This manifest.mf can be placed in any location, or it can be another file name, only need to have main-class:test. Test line, and the line ends with a carriage return character. After creating the MANIFEST.MF file, our directory structure becomes:

==

|--Test

| '--Test.class

'--MANIFEST.MF

At this point, you need to go to the parent directory of the test directory to use the JAR command to create the jar file package. That is, in the directory tree using the "= =" In that directory, use the following command:

Jar CVFM Test.jar MANIFEST.MF test

The Test.jar is then created in the "= =" directory, and this test.jar is the jar file package that is executed. The runtime only needs to use the Java-jar test.jar command.

It is important to note that the JAR package created needs to contain the complete directory structure corresponding to the Java program's package structure, as in the previous example. The class specified by Main-class must also be a complete class name that contains the package path, as in the previous example, test. Test, and you can use Java < class name > to run this class without hitting the JAR package, which is the Java test in the previous example. Test can be run correctly (of course, in the CLASSPATH correct case).

class files and jar files in Java

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.