Java manifest file

Source: Internet
Author: User
Tags log4j

 Java jar PackagingTags: javajarmanifestmain-classclass-path2016-03-30 00:29 993 People read comments (0) favorite reports Classification:Java (6)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Directory (?) [+]

1. Introduction to Jar

The Java Archive file format (Java Archive, JAR) can package multiple sources, resources, and other files into an archive file. In this way, there are the following benefits:

    • Security
      The contents of the entire JAR package can be signed.
    • Reduced download time
      If the applet is packaged as a jar file, all related resources can be downloaded in an HTTP transaction without having to create a new connection for each file.
    • Compression
      Reduced disk space usage.
    • Easy to expand
      Through the jar format, you can and easily package your own programs for others to use.
    • Bag Seal (Package sealing)
      Packages stored in JAR files can be sealed to ensure version consistency. Sealing guarantees that all classes in a package are from the same jar file.
    • Package Release Notes
      A jar package can store information about its contents, including providers, versions, and so on.
    • Portability
      The mechanism for working with jar files is the standard module of the Java Platform Core API.

From docs.oracle.com

2, the use of the jar

The package tool that comes with the JDK jar is called by command, and the jar is packaged in zip format. Therefore, this jar tool can also be used as a daily compression and decompression tool.

2.1 Jar Command Description

If you install the JDK and configure the environment variables, enter the command at the command line jar , without any parameters, you can see the instructions for using the Jar command, and the following two examples are included:

jar用法: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...选项:    -c  创建新档案    -t  列出档案目录    -x  从档案中提取指定的 (或所有) 文件    -u  更新现有档案    -v  在标准输出中生成详细输出    -f  指定档案文件名    -m  包含指定清单文件中的清单信息    -n  创建新档案后执行 Pack200 规范化    -e  为捆绑到可执行 jar 文件的独立应用程序指定应用程序入口点    -0  仅存储; 不使用任何 ZIP 压缩    -P  保留文件名中的前导 ‘/‘ (绝对路径) 和 ".." (父目录) 组件    -M  不创建条目的清单文件    -i  为指定的 jar 文件生成索引信息    -C  更改为指定的目录并包含以下文件如果任何文件为目录, 则对其进行递归处理。清单文件名, 档案文件名和入口点名称的指定顺序与 ‘m‘, ‘f‘ 和 ‘e‘ 标记的指定顺序相同。示例 1: 将两个类文件归档到一个名为 classes.jar 的档案中:       jar cvf classes.jar Foo.class Bar.class示例 2: 使用现有的清单文件 ‘mymanifest‘ 并将 foo/ 目录中的所有文件归档到 ‘classes.jar‘ 中:       jar cvfm classes.jar mymanifest -C foo/摘自<jar命令的帮助文档>
2.2 Jar Use Example

When the jar command is packaged, the manifest (manifest) file is added to the jar package by default, and if you do not want to add it, specify the -M option manually:

>jar -cvf HelloWorld.jar HelloWorld.class   #将HelloWorld.class文件打入jar包    已添加清单    正在添加: HelloWorld.class(输入 = 427) (输出 = 289)(压缩了 32%)>jar -tf HelloWorld.jar   #查看归档文件的内容    META-INF/    META-INF/MANIFEST.MF    HelloWorld.class>jar -xf HelloWorld.jar META-INF/MANIFEST.MF   #解压出其中的META-INF/MANIFEST.MF文件>type META-INF\MANIFEST.MF   #查看清单文件的内容    Manifest-Version: 1.0    Created-By: 1.8.0_51 (Oracle Corporation)>jar -cvfM HelloWorld.jar HelloWorld.class   #将HelloWorld.class文件打入jar包,不要添加清单文件    正在添加: HelloWorld.class(输入 = 427) (输出 = 289)(压缩了 32%)>jar -tf HelloWorld.jar    HelloWorld.class>jar -xf HelloWorld.jar   #解压tar文件到当前目录

If you want to build a jar package that can run, you need to specify the application entry point for the jar package, with the-e option:

>jar -cvfe HelloWorld.jar HelloWorld HelloWorld.class   #创建可以运行的jar包    已添加清单    正在添加: HelloWorld.class(输入 = 427) (输出 = 289)(压缩了 32%)>jar -tf HelloWorld.jar   #查看归档的内容    META-INF/    META-INF/MANIFEST.MF    HelloWorld.class>type META-INF\MANIFEST.MF   #查看清单文件的内容    Manifest-Version: 1.0    Created-By: 1.8.0_51 (Oracle Corporation)    Main-Class: HelloWorld>java -jar HelloWorld.jar   #运行jar包    Hello World!!
3. manifest file MANIFEST.MF

When the jar is packaged, a manifest file (MANIFEST) is added to the jar package by default. MF), placed in the Meta-inf directory. In the example above, you can see that if you specify a jar package entry program, there will be more than one row in the manifest file Main-Class: HelloWorld . In fact, -e the purpose of the option is to add such a line of information to the MANIFEST.MF file to declare the JAR package's entry procedure. Of course, we can also directly modify the contents of the manifest file.

3.1 Modifying the contents of a manifest file

As already mentioned, -m options can be added to the manifest information in the specified manifest file in the following format:

jar cfm jar-file manifest-addition input-file(s)

Attention:

    • Manifest-addition is a text file that already exists that contains the inventory information that you want to add to the manifest file for the jar package.
    • The file encoding format for manifest-addition must be UTF-8.
    • The manifest information must be followed by a carriage return or line break after each line. (The text file from which you is creating the manifest must end with a new line or carriage return.) The last line is not being parsed properly if it does not end with a new line or carriage return.)
    • The manifest file name, and the specified order of the file and entry point names are the same in the specified order of ' m ', ' f ' and ' e ' tags. (already mentioned earlier)
3.2 Declaring the entry program in the manifest file
>type manifest.txt   #查看清单信息的内容    Main-Class: HelloWorld>jar -cvfm HelloWorld.jar manifest.txt HelloWorld.class   #添加清单信息到jar包    已添加清单    正在添加: HelloWorld.class(输入 = 427) (输出 = 289)(压缩了 32%)>jar -xvf HelloWorld.jar META-INF\MANIFEST.MF      已解压: META-INF/MANIFEST.MF>type META-INF\MANIFEST.MF    Manifest-Version: 1.0    Created-By: 1.8.0_51 (Oracle Corporation)    Main-Class: HelloWorld   #可以看到清单信息成功添加>java -jar HelloWorld.jar   #这样指定入口程序,生成的jar包照样可以运行。    Hello World!!
3.3 Specifying Classpath in the manifest file

When the java command is run, the -jar environment variable classpath and all classpath specified on the command line are ignored by the JVM if an option is specified. At this point, if a jar package references other jar packages, there are two solutions:

  1. java -cp lib\log4j-1.2.14.jar;hello.jar com.dhn.Hello(Com.dhn.Hello main Class)
    Separated by a semicolon (;) between multiple jars under Windows, you need to specify the full main class name in the Run JAR file.

  2. java -jar hello.jar
    In this case, you need to modify the MANIFEST.MF in the Hello.jar to specify additional jars to be used by the runtime through the Class-path in MANIFEST.MF, and the other jars can be either the current path or a subdirectory under the current path. Multiple jar files are separated by a space.
    Take the following MANIFEST.MF file as an example

    Manifest-Version: 1.0Main-Class: com.ibm.portalnews.entrance.MainClass-Path: lib\commons-collections-3.2.jar lib\commons-configuration-1.5.jar lib\commons-lang-2.3.jar lib\commons-logging.jar lib\dom4j-1.6.1.jar lib\jaxen-1.1-beta-7.jar lib\jdom.jar lib\log4j-1.2.14.jar

    which

    • Manifest-version represents the version number, which is typically generated automatically by IDE tools such as Eclipse
    • Main-class is the main class of the jar file, the entry of the program
    • CLASS-PATH Specifies the required jar, multiple jars must be on one line, and multiple jars are separated by a space, if the referenced jar is in the subdirectory of the current directory, Windows uses \ to split, Linux down/split
    • The file must have a blank space after the colon, or an error will occur
    • The last line of the file must be a carriage return line break, or else an error will occur

    From:java-jar Classpath Experience

3.4 Pack The other jar packages referenced by jar A into a

That's the question I've been thinking about. If so, it's much easier to copy a program around. However, this does not work because the JDK ClassLoader does not load classes from other jar packages contained within the JAR package.

Of course, I am not the only one who has thought about it, and can do it through some third-party tools. For example, there is a tool called One-jar, interested can be seen.

3.5 Sealing packages with manifest files

The package sealing feature in the jar was introduced in Java 1.2. When generating a jar file, we can specify whether to seal the entire jar or some of the package, and if the jar file is sealed, it means that all of its contents are sealed. Once the package is sealed, once the Java virtual machine has successfully loaded a class in the sealed package, all classes loaded with the same package name must be from the same jar file, or the sealing violation security exception will be triggered.

From the Java Package Sealing Quest Tour

There are two main ways to seal a package: one is to seal some of the packages in the jar file, and the other is to seal all the packages in the jar.

    • Seal on certain packages.

      Manifest-Version: 1.0Created-By: 1.8.0_51 (Oracle Corporation)Name: com/test/hello/  Sealed: trueName: com/test/world/  Sealed: true  
    • Seal all packages in the jar.

      Manifest-Version: 1.0Created-By: 1.8.0_51 (Oracle Corporation)Sealed: true

In summary, there are two situations that trigger a security exception about sealing:
1. Check whether the package for the class currently attempting to load is already loaded and sealed by the JVM. If it has been loaded and sealed, the package that corresponds to the currently loaded class is not from the same jar file and will trigger a security exception.
2. Check whether the package for the class currently attempting to load is already loaded and sealed by the JVM. If it has been loaded but not sealed, the package of the class that is currently attempting to load will attempt to seal the operation, triggering a security exception. The JVM does not allow the sealing of a package that is already loaded but not sealed.

the benefits of package sealing:
The benefits of package sealing are primarily version consistency. We know that Java is loaded and checked strictly in the order defined in Classpath, especially now that the Java Open source package is running It is likely that your Java application or the classpath of your middleware will contain different versions of the same package in different jar files. This makes the program run with inconsistent results and is difficult to find.

From:java in the package sealing tour

Java manifest file

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.