gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:46:27 41 4
gpt4 key购买 nike

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

JAR 文件是模块吗?

模块与 JAR 文件有何不同?

最佳答案

模块:Java 9 中引入的一种新的语言 特性(类似于类、接口(interface)、包等),由包的集合组成,类似于如何一个包由一组类型组成。

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. 模块可以决定将哪些类和接口(interface)导出到需要它的其他模块。 Jar 没有这样的封装机制。

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

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

关于java - 模块和 JAR 文件有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46513903/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com