原创翻译 JAR包格式的介绍 自压原创小格式

JAR简介

Jar文件基于ZIP格式,用于将多个文件打包成单一文件。Jar文件本质上就是ZIP文件改了个名,并可以包含可选的META-INF目录。可以在命令行通过jar命令创建jar文件,或者在编程中使用java.util.jar中的API方法。Jar文件的名字怎么起都可以。

JAR file is a file format based on the popular ZIP fileformat and is used for aggregating many files into one.A JAR file is essentially a zip file thatcontains an optional META-INF directory. A JAR file can be createdby the command-line jar tool, or by using the java.util.jar API in the Java platform. There is no restrictionon the name of a JAR file, it can be any legal file name on aparticular platform.

很多情况下,JAR文件不但可以打包java类和程序资源,还可以用于构建应用程序的组件,如果存在META-INF目录,它可以用来存储扩展的配置信息,比如安全、版本、服务等。

In many cases, JAR files are not just simple archives of javaclasses files and/or resources. They are used as building blocksfor applications and extensions. The META-INF directory, if itexists, is used to store package and extension configuration data,including security, versioning, extension and services.

META-INF目录的内容

Java 2平台可以通过META-INF目录的如下文件进行配置:

The following files/directories in the META-INF directory arerecognized and interpreted by the Java 2 Platform to configureapplications, extensions, class loaders and services:

此文件用来定义附加的信息

The manifest file that is used to define extension andpackage related data.

在使用jar工具时添加-i参数可以生成此文件,它包含了文件的位置索引信息。类加载器可以利用此信息提高加载速度

This file is generated by the new "-i" option of the jartool, which contains location information for packages defined inan application or extension. It is part of theJarIndex implementation and used by class loaders to speed up theirclass loading process.

JAR包的签名文件,x代表文件名

The signature file for the JAR file. 'x'stands for the base file name.

与签名文件关联的签名块儿文件(通过文件名x关联),此文件中存储了签名文件的数字签名信息。

The signature block file associated with the signature filewith the same base file name. This file stores the digitalsignature of the corresponding signature file.

此目录存放所有的服务提供者的配置文件

This directory stores all the service provider configurationfiles.

键值对Name-Value pairs and Sections

在介绍各个配置文件的细节之前,需要定义一些格式规范。在大多数情况下,配置文件中的内容为键值对格式。一组键值对称为段(section),多个段之间用空行分割。

Before we go to the details of the contents of the individualconfiguration files, some format convention needs to be defined. Inmost cases, information contained within the manifest file andsignature files is represented as so-called "name: value" pairsinspired by the RFC822 standard. We also callthese pairs headers or attributes. Groups of name-value pairs areknown as a "section". Sections are separated from other sections byempty lines.

二进制数据使用BASE64格式,会导致一行超出72个字节。比如存放数字签名,要求解析器至少支持64K的长度。下文中终端符号使用等宽字体,非终端符--号使用斜体。

Binary data of any form is represented as base64.Continuations are required for binary data which causes line lengthto exceed 72 bytes. Examples of binary data are digests andsignatures.

Implementations shall support header values of up to 65535bytes.

All the specifications in this document use the same grammar inwhich terminal symbols are shown in fixed width font andnon-terminal symbols are shown in italic type face.

Specification(这个就不翻译了吧):

section:*header +newline
nonempty-section:+header +newline
newline:CR LF | LF | CR (not followed by LF)
header:name : value
name:alphanum *headerchar
value:SPACE *otherchar newline *continuation
continuation:SPACE *otherchar newline
alphanum:{A-Z} | {a-z} | {0-9}
headerchar:alphanum | - | _
otherchar:any UTF-8 character except NUL, CR and LF

; Also: To prevent mangling of files sent via straighte-mail, no
; header will start with the four letters "From".

Non-terminal symbols defined in the above specification will bereferenced in the following specifications.

JAR文件的说明 JAR Manifest

概览Overview

Jar文件的说明包括一个主要段和之后的多个段,用空格分隔。这些段的格式都符合上面所说的规范。他们各自有各自的约束与规则。

主段包含JAR文件的安全和配置信息,同时也定义了主要属性。段中的属性名不能叫Name,当遇到空行时本段结束。

A JAR file manifest consists of a main section followed by alist of sections for individual JAR file entries, each separated bya newline. Both the main section and individual sections follow thesection syntax specified above. They each have their own specificrestrictions and rules.

The main section contains security and configurationinformation about the JAR file itself, as well as the applicationor extension that this JAR file is a part of. It also defines mainattributes that apply to every individual manifestentry. No attribute in this section can have itsname equal to "Name". This section is terminatedby an empty line.

其他段为JAR文件中的其他包或文件定义了各种属性,不是JAR包中所有的文件都需要在声明中罗列出来,但是需要签名的文件必须罗列。声明文件本身不能列出,每个段必须以Name属性开头,值为文件名的相对路径,或绝对的URL。

如果一个属性有多个值,他们会合并。如果多个属性在多个段中有不同的值,会取最后一个。不能被程序识别的属性会被忽略。

例子:微软的SQL Server 2005驱动jar包中META-INF/MANIFEST.MF的节选:

Manifest-Version: 1.0

Ant-Version: Apache Ant 1.6.2

Created-By: 1.5.0_15-b04 (Sun Microsystems Inc.)

Name: com/microsoft/sqlserver/jdbc/SQLServerException.class

SHA1-Digest: tAQNyqt3kNpnn39kR7KOyEZN0Is=

Name:com/microsoft/sqlserver/jdbc/JDBCType$UpdaterConversion.class

SHA1-Digest: 5eQKiIrZa1Cd6QeIVaZCCC0qJqA=

The individual sections define various attributes forpackages or files contained in this JAR file. Not all files in theJAR file need to be listed in the manifest as entries, but allfiles which are to be signed must be listed. The manifest fileitself must not be listed. Each section muststart with an attribute with the name as "Name", and the value mustbe a relative path to the file, or an absolute URL referencing dataoutside the archive.

If there are multiple individual sections for the same fileentry, the attributes in these sections are merged. If a certainattribute have different values in different sections, the last oneis recognized.

Attributes which are not understood are ignored. Suchattributes may include implementation specific information used byapplications.

Manifest Specification:

manifest-file:main-section newline *individual-section
main-section:version-info newline *main-attribute
version-info:Manifest-Version : version-number
version-number:digit+{.digit+}*
main-attribute:(any legitimate main attribute) newline
individual-section:Name : value newline *perentry-attribute
perentry-attribute:(any legitimate perentry attribute) newline
newline:CR LF | LF | CR (not followed by LF)
digit:{0-9}

In the above specification, attributes that can appear in themain section are referred to as main attributes, whereas attributesthat can appear in individual sections are referred to as per-entryattributes. Certain attributes can appear both in the main sectionand the individual sections, in which case the per-entry attributevalue overrides the main attribute value for the specified entry.The two types of attributes are defined as follows.

MainAttributes

主要属性出现在主段中,分为下面几个组:

Main attributes are the attributes that are present in themain section of the manifest. They fall into the followingdifferent groups:

Manifest-Version:声明文件的版本

Created-By: 创建者

Signature-Version:签名版本

Class-Path:(重要)本项指定了引用的其他资源,使用相对路径表示。多项资源之间用空格分隔。应用程序或加载器根据此项值加载相关的资源。比如GlassfishV3中的javaee.jar和appserv-rt.jar就采用了此属性。

Main-Class :通过java –jar执行jar包时启动的类名,不需要包含.class扩展名

(鉴于Applet基本过时,此段删除)

Extension-Name:唯一的字符串

Implementation-Title:标题

Implementation-Version:版本

Implementation-Vendor:制造商

Implementation-Vendor-Id:制造商ID

Implementation-URL:下载地址

Specification-Title:规范标题

Specification-Version:版本

Specification-Vendor:制造商

关于加封的解释百度了一段:

从JAVA2开始,Java引入了Seal(加封)安全机制。所有的JAR文件和包都可以选择是否需要加封,如果JAR文件被加封,则等价于JAR文件中的所有包被加封。一旦被加封的包中的某个类被引用,则规定任何以后对这个包中的成员的引用都必须在同一个JAR文件中。从而保证了版本的一致性。如果违反了上述Seal机制,则我们就将遇到SealingViolation错误。

  

爱华网本文地址 » http://www.413yy.cn/a/25101015/258424.html

更多阅读

花式包书皮的方法 用纸包花式书皮的步骤

花式包书皮的方法——简介上学期间有一件事总是懊恼着我们,就是书的封面总爱翘起来,因此一学期读完,书就烂的不成样子了,看着就很不舒服,你是否很羡慕那些书本如新的同学了,那就看过来,我教你折一款又简单又漂亮的书皮,让你的书始终如新。

来自安卓手机QQ自定义尾巴编辑的做法 自定义伪装微博尾巴

想必各位机友都很羡慕苹果手机或ipad登录QQ会显示来自QQfor iPhone的尾巴吧?那就是,当自己或者别人用手机登入QQ,并开始聊天的时候,在聊天内容的下方会出现这样一行文字:“(来自QQ for Andriod)”或者“(来自QQ for iPhone)”或者“(来自QQ f

httpwatch抓包工具的使用方法 铁夹子夹野兔安装图片

httpwatch抓包工具的使用方法——简介火狐浏览器下有著名的httpfox,而HttpWatch则是IE下强大的网页数据分析工具。这个工具到底有哪些具体功能呢?这个我就不再赘述了,百度百科上列的很全面,但也比较抽象。我只想说我曾经用这个工具去分

跳蚤的介绍,危害,判断方式以及防治方法 怎样防治跳蚤

跳蚤的介绍,危害,判断方式以及防治方法——简介夏天来了,宠物的体外寄生虫也渐渐的多了起来,其中最常见也最让人不太在意的就应该是跳蚤了。常见是因为经常发生,不在意是因为见得比较平常。其实跳蚤所带来的危害往往比主人知道的要多和严

怎么更改文件夹的图标(自定义文件夹图标) 自定义文件夹图标

怎么更改文件夹的图标(自定义文件夹图标)——简介打开电脑,我们看到的文件夹都是同一种图标,也许我们会觉得没有美感,没个性!要是能够更改成自己喜欢的图标就好了,那样的话,看起来就爽多了!那么怎么样才能更改成自己喜欢的图标呢?下面小编

声明:《原创翻译 JAR包格式的介绍 自压原创小格式》为网友挑眉少年分享!如侵犯到您的合法权益请联系我们删除