Learn how to use manifest

Source: Internet
Author: User

The day before yesterday, my boss asked me to add my previously written inference engine to the so-called knowledge management software left by my lab Senior brother and sister to facilitate this demonstration on Friday. How can this problem be solved? It is difficult to run the task after a tight schedule, but it is found that there is still a problem during packaging and publishing. It is estimated that there is a conflict between multiple main-classes during the packaging process? I found an article on manifest configuration on the Internet. I saved it and hoped it could be used.

 

====================

The following is a reference, see: http://www.blogjava.net/huanghuizz/articles/163379.html

 

Simplify Java application packaging and Publishing
How difficult do you feel when releasing Java applications? Fortunately, Java provides a series of packaging and publishing tools, which can significantly simplify the publishing process.
This article provides several methods for packaging Java code. We will discuss the Java manifest file, and provide a suitable method for managing the files dependent on jar files and 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?

During the development process, we can directly use the Java class file to run the program, but this is not a good method. Fortunately, Java provides the jar (Java archive) file for release and running.

Jar files are actually zip archives of class files. This format is widely used, so it is easy to use. There are many tools that can operate files in this format. For this reason, the JAR file itself cannot express the tag information of the included application.

Therefore, manifest appears.

To provide archived tag information, the JAR file specifies a specific directory to store Tag Information: META-INF
Directory, in which we will follow the manifest. MF file in this directory, which is the manifest file of jar, which contains the content description of the JAR file, and submit it to JVM at runtime
Information about the supplied program. Most jar files contain a default generated manifest file, which can be generated by executing the jar command or using the zip tool.

If the manifest file is generated by the jar command, the format is as follows:

Manifest-version: 1.0
Created-by: 1.4.0-beta
(Sun Microsystems Inc .)

This information is useless. We only use the 1.0 manifest file, the first line defines the manifest format, and the second line describes the use of sun
The jar tool of jdk1.4 generates this file. If the manifest file is created by another (such as ant), "created-by: ant" will appear.
1.2 "and so on. If you create a manifest file by yourself, you can add some related information.

Basic Format

The format of the manifest file is very simple. Each line corresponds to a name-value: the beginning of the attribute name, followed by ":", and then the attribute value. Each line can contain up to 72 characters, if you want to add a new row, you can continue the row in the next row. The new row starts with a space and all the rows starting with a space are considered as the previous row.

All attributes at the beginning are global. You can also define the attributes of a specific class or package.

Insert 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 use ant to create an object, add the following entries in ant build. xml:

<Target name = "jar">
<Jar jarfile = "myapplication. Jar"
Manifest = "myapplication. MF">
<Fileset dir = "classdir"
Primary des = "**/*. Class"/>
</Jar>
</Target>

Run Java program

Now let's try out the functions of the manifest file. If we have a Java application packaged in myapplication. in jar, main class is com. example. myApp. myappmain, then we can run the following command

Java-classpath myapplication. Jar com. example. MyApp. myappmain

This is obviously too troublesome. Now we can create our own manifest file, as shown below:

Manifest-version: 1.0
Created-by: jdj example
Main-class: COM. example. MyApp. myappmain

In this way, we can use the following command to run the program: (obviously much simpler, it will not cause unnecessary spelling errors)

Java-jar myapplication. Jar

Manage jar dependent resources

Few Java applications only have one jar file, and other class libraries are generally required. For example, my application uses Sun's javamail classes.
In classpath, I need to include activation. jar and mail. Jar. In this way, when running the program, we need to add some more:

Java-classpath mail. jar: Activation. Jar-Jar myapplication. Jar

In different operating systems, the separators between jar packages are different, and ":" is used in UNIX and ";" is used in window, which is inconvenient.

Similarly, 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, separated by spaces)

In this way, we can still execute the program using the same command as in the previous example:

Java-jar myapplication. Jar

The class-path attribute contains jar files separated by spaces. escape characters must be used for specific characters in these jar files, such as spaces, to be expressed as "% 20 ", path Representation
"/" Is used to separate directories, regardless of the Operating System (even in the window), and the relative path (relative to the JAR file) is used here):

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 (multiple main classes)
There is also a case of multiple main classes, if your application may have a command line version
And the GUI version, or some different applications share a lot of the same Code, you may have multiple main
Class. We recommend that you use the following policy: compress the shared class into a lib package, compress different applications into different packages, and mark the main class separately:

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 manifest files of myappconsole. jar and myappadmin. jar, specify their main class respectively.

Package Versioning

After the release is completed, if you want to know who the code is? What is the current version? What version of the class library is used? There are many solutions. manifest provides a better solution. You can describe the information of each package in the manifest file.

Java adheres to the principle of separation of implementation description and description. The description of a package defines what a package is.
Defines who provides the description implementation, description and implementation including name, version number, and provider. To obtain this information, you can view the JVM system attributes (using
Java. Lang. system. getproperty ())

In the manifest file, I can define the description and implementation version for each package, declare the name, and add the description and implementation attributes. These attributes are

Specification-title
Specification-version
Specification-vendor
Implementation-title
Implementation-version
Implementation-vendor

When you want to provide a class library or programming interface, the description information is very important. See 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-
Implementation-vendor: example.com

Query package version
After the package description is added to the manifest file, you can use Java. Lang. Package class provided by Java to query package information. Here are three basic methods for obtaining package objects.

1. Package. getpackages (): returns the list of all defined packages in the system.
2. Package. getpackage (string packagename): return package by name
3. Class. getpackage (): returns the package of the given class.

This method allows you to dynamically obtain package information.
Note that if no class in the specified package is loaded, the package object cannot be obtained.

Manifest skills
Always starting with the manifest-version attribute

Each line can contain up to 72 characters.

Confirm that each line ends with a carriage return. Otherwise, the line will be ignored.

If a path exists in class-path, use "/" to separate directories, regardless of the platform.

Use empty rows to separate the primary attributes and package attributes

Use "/" instead of "." To separate package and class, such as COM/example/MyApp/

The class should end with. class, and the package should end/

 

 

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.