模块和JAR文件有什么区别?

2024-04-24 16:25:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从What's New in Java9学习Java 9,讨论中的一个热门话题是模块化JDK

JAR文件是模块吗?

模块与JAR文件有何不同?


Tags: 模块文件innewjava模块化whatjar
3条回答

严格地说,模块是一个运行时概念。正如其他人从The State of the Module system引述的那样:

A module is a named, self-describing collection of code and data. Its code is organized as a set of packages containing types, i.e., Java classes and interfaces; its data includes resources and other kinds of static information.

这与JARs非常相似,但是。。。

  1. jar在运行时没有有意义的表示
  2. jar不是“自描述的”,在本例中,这意味着它们没有JVM关心的名称,不能表示依赖关系或定义适当的API

这就留下了一个问题,模块从何而来?有多种方法,但对于开发人员来说最突出的是模块化JAR。模块化JAR类似于普通JAR,但它包含一个模块描述符,一个从module-info.java编译而来的文件module-info.class。就是那个文件defines a module's name, dependencies, and APIs

因此,JAR和模块之间有着紧密的联系:JAR是模块系统创建模块的容器,而(目前)每个JAR只能包含一个模块。需要注意的是,即使在Java 9上,jar也不能具有模块化-普通jar完全可以。

JAR文件是模块吗?模块与JAR文件有何不同?

不,aJava Archive不是模块

举个例子,当类 the same package could have been spread across JARs,同一个包现在can not be read from multiple modules.

A JAR is a file format that enables you to bundle multiple files into a single archive file. Typically this contains the class files and auxiliary resources associated with applets and applications.

另一方面(我也试过在这里描述这个~>;

A module is a named, self-describing collection of code and data. Its code is organized as a set of packages containing types, i.e., Java classes and interfaces; its data includes resources and other kinds of static information.

这还包括在module-info.java帮助下指定的模块声明。

每个模块定义都是

  • A模块工件,即包含编译模块定义的模块JAR文件或JMOD文件,否则

  • 一种分解的模块目录,按惯例,其名称是模块的名称,其内容是与包层次结构相对应的“分解”目录树。

正如模块系统所介绍的,模块映像由模块而不是JAR文件组成。 根据Modular WAR file as well.的动态配置可以预见模块性


但是为了便于采用模块,JDK9中引入了Modular JAR file,,例如,对于由module-info.java组成的模块

module com.foo.bar { }

以及其他java类

com/foo/bar/alpha/AlphaFactory.java
com/foo/bar/alpha/Alpha.java

Existing tools can already create, manipulate, and consume JAR files. A modular JAR file is like an ordinary JAR file in all possible ways, except that it also includes a module-info.class file in its root directory. A modular JAR file for the above com.foo.bar module, e.g., might have the content:

META-INF/
META-INF/MANIFEST.MF
module-info.class
com/foo/bar/alpha/AlphaFactory.class
com/foo/bar/alpha/Alpha.class
...

A modular JAR file can be used as a module, in which case its module-info.class file is taken to contain the module’s declaration. It can, alternatively, be placed on the ordinary class path, in which case its module-info.class file is ignored.

模块:Java 9中引入的一种新的语言功能(类似于类、接口、包等),它由一组包组成,类似于包如何由一组类型组成。

JAR:打包代码和资源并可由JVM加载的存档文件格式。

更具体地说,模块是defined as follows

In order to provide reliable configuration and strong encapsulation in a way that is both approachable to developers and supportable by existing tool chains we treat modules as a fundamental new kind of Java program component. A module is a named, self-describing collection of code and data. Its code is organized as a set of packages containing types, i.e., Java classes and interfaces; its data includes resources and other kinds of static information.

...

A module’s self-description is expressed in its module declaration, a new construct of the Java programming language.

...

A module declaration is compiled, by convention, into a file named module-info.class, placed similarly in the class-file output directory.

模块可以编译成Jar文件,在这种情况下,Jar文件被标记为模块Jar文件:

Existing tools can already create, manipulate, and consume JAR files, so for ease of adoption and migration we define modular JAR files. A modular JAR file is like an ordinary JAR file in all possible ways, except that it also includes a module-info.class file in its root directory.

模块和JAR之间的一些其他区别:

  1. 模块可以需要其他模块,以便允许需要的模块访问依赖类。Jar没有这样的依赖概念。

  2. 模块可以决定将哪些类和接口导出到需要它的其他模块。Jar没有这样的封装机制。

  3. 一个模块可以编译成一个模块Jar文件,但是一些模块(例如JDK模块)被编译成另一种称为JMOD的格式。

  4. 可以更改JAR的名称。只要JVM类加载器在类路径上找到所需的类(可以由单个JAR、多个JAR或目录或JAR的组合组成),JAR文件的名称就可以是任何内容。但是,一个模块的名称可以在其他模块的声明中显式引用,这样的名称定义了它,不能随意更改。

相关问题 更多 >