The role of MANIFEST.MF in Meta-inf in Java Projects (reproduced)

Source: Internet
Author: User

Do you find it difficult to publish Java applications? Fortunately, Java provides a range of packaging and publishing tools that can significantly simplify the publishing process


This article provides several ways to package Java code, and we will explore the Java manifest file, giving you a file to manage the files that the jar file depends on, An appropriate method for estimating the classpath required for cross-platform publishing. I will also explain how to use the Manifest Package version feature to confirm package compatibility ...

What is a jar file?

In the development process, we can run the program directly using the Java class file, but this is not a good way, fortunately Java provides a JAR (Java Archive) file to provide publishing and running.

The jar file is actually a zip archive of class files, this format is widely used, so easy to use, there are many tools in this format can manipulate files. It is for this reason that the jar file itself does not convey the tag information of the included application.

Manifest is thus able to appear
In order to provide archived label information, the jar file specifies a specific directory to hold the tag information: the Meta-inf directory, Here we look at the MANIFEST.MF file in this directory, he is the jar's manifest file, he contains the contents of the jar file description, and at run time to the JVM to provide information about the application, most of the jar file contains a default generated manifest file, Execute the JAR command or use the zip tool to generate it
If it is a manifest file produced by the jar command, the shape is:
manifest-version:1.0
Created-by:1.4.0-beta
(Sun Microsystems Inc.)
This information is useless, just tell us to use the 1.0 manifest file, the first line defines the format of the manifest, the second line describes the Jar tool that uses SUN's JDK1.4 to generate the file, if the manifest file was created by another (such as Ant), There will be something like "created-by:ant 1.2", and if you create a manifest file yourself, you can add some information about yourself.

Basic format

The format of the manifest file is very simple, each line is a name-value corresponding to: The property name begins, followed by ":", then the property value, each line up to 72 characters, if necessary, you can continue the line on the next line, the continuation of the line begins with a space, the line beginning with a space will be considered as the continuation of the
All the attributes at the beginning are global, you can also define the properties of a particular class or package, which will be described later
Inserting the manifest file into the jar file
Use the M option to pass in the manifest file of the specified file name, for example
Jar CVFM Myapplication.jar myapplication.mf-c classdir

If you are using ant to create, add the following entry in Ant's Build.xml


manifest= "MYAPPLICATION.MF" >

includes= "**/*.class"/>


Running Java programs
Now let's take a look at the role of the manifest file, and if we now have a Java application packaged in Myapplication.jar, the main class is Com.example.myapp.MyAppMain, Then we can use the following command to run
Java-classpath Myapplication.jar Com.example.myapp.MyAppMain
This is obviously too troublesome, now let's create our own manifest file, as follows:
manifest-version:1.0
CREATED-BY:JDJ Example
Main-class:com.example.myapp.myappmain
This way we can use the following command to run the program: (obviously more simple, and will not cause unnecessary spelling mistakes)
Java-jar Myapplication.jar
Managing dependency Resources for Jars
Very few Java applications will have just one jar file, and typically require other class libraries. For example, my application uses Sun's Javamail classes, and in Classpath I need to include Activation.jar and Mail.jar, so when we run the program, we'll add some more than the above example:
Java-classpath Mail.jar:activation.jar-jar Myapplication.jar
In different operating systems, the delimiter between jar packages is not the same, in Unix with ":", in the window using ";", it is not convenient
Again, we rewrite our manifest file as follows
manifest-version:1.0
CREATED-BY:JDJ Example
Main-class:com.example.myapp.myappmain
Class-path:mail.jar Activation.jar
(Added Class-path:mail.jar Activation.jar, two jar packages separated by spaces)
This way we can still execute the program using the same command as in the example above:
Java-jar Myapplication.jar
The Class-path property contains a space-delimited jar file in which to use escape characters for specific characters, such as spaces, to be represented as "", and "/" to separate directories in the representation of a path, regardless of the operating system, (even in Windows), And here's a relative path (relative to its own jar file):
manifest-version:1.0
CREATED-BY:JDJ Example
Main-class:com.example.myapp.myappmain
Class-path:ext/mail.jar Ext/activation.jar
Multiple main Classes (multi-master Class)
There is also a multiple main classes case where you might have multiple main classes if your application might have command-line versions and GUI versions, or if some different applications share many of the same code. We recommend that you take this strategy: the shared class into the Lib package, and then the different applications into different packages, the main class is marked as follows:
Manifest for Myapplicationlib.jar:
manifest-version:1.0
CREATED-BY:JDJ Example
Class-path:mail.jar Activation.jar
Manifest for Myappconsole.jar:
manifest-version:1.0
CREATED-BY:JDJ Example
Class-path:myapplicationlib.jar
Main-class:com.example.myapp.myappmain
Manifest for Myappadmin.jar:
manifest-version:1.0
CREATED-BY:JDJ Example
Class-path:myapplicationlib.jar
Main-class:com.example.myapp.myadmintool
In the Myappconsole.jar and Myappadmin.jar manifest files, separate the Main Class
Package Versioning
After publishing, if the user wants to know, which code is who? What is the current version? What version of the class library do you use? There are many workarounds, and manifest provides a better way to describe each package in the manifest file.
Java adheres to the principle of separating the implementation description from the description, and the package description defines what the package is, and the implementation description defines who provided the description of the implementation, description and implementation of the include name, version number, and provider. To get this information, you can view the system properties of the JVM (using Java.lang.System.getProperty ())
In the manifest file, I can define a description and implementation version for each package, declare the name, and add a description attribute and implementation attribute, which are
Specification-title
Specification-version
Specification-vendor
Implementation-title
Implementation-version
Implementation-vendor
When it comes to providing a class library or programming interface, it is important to describe the information, as shown in the following example:
manifest-version:1.0
CREATED-BY:JDJ Example
Class-path:mail.jar Activation.jar
name:com/example/myapp/
Specification-title:myapp
specification-version:2.4
Specification-vendor:example.com
Implementation-title:com.example.myapp
Implementation-version:2002-03-05-a
Implementation-vendor:example.com
Package Version Query
After adding the package description in the manifest file, you can use the Java.lang.Package class provided by Java to query for the information about the 3 most basic methods of acquiring the
1.package.getpackages (): Returns all defined package listings in the system
2.package.getpackage (String PackageName): Return to package by name
3.class.getpackage (): Returns the package containing the given Class
Users can get the package information dynamically by using this method.
It is important to note that if no class is loaded in the given package, the package object cannot be obtained

Manifest Tips
Always start with the Manifest-version property
Up to 72 characters per line, if more than one, the continuation line is used
Verify that each line ends with a carriage return or the row will be ignored
If there is a path in Class-path, use "/" to separate the directory, regardless of the platform
Separating main and package properties with blank lines
Use "/" instead of "." To separate the package and class, such as com/example/myapp/
class  to end with a. Class,package  to end with  / 

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.