Java 9 - “模块”之间的区别是什么?和“JAR”文件? [英] Java 9 - What is the difference between "Modules" and "JAR" files?

查看:162
本文介绍了Java 9 - “模块”之间的区别是什么?和“JAR”文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Java9中的新功能中学习Java 9讨论中的一个热门话题是
模块化JDK

I am learning about Java 9 from What's New in Java9 and one of the hot topic in the discussion is The Modular JDK.

我有些疑惑:


  1. JAR文件是模块吗?

  2. 模块与JAR文件有何不同?


推荐答案

模块:Java 9中引入的新语言功能(类似于class,interface,package等)由一组包组成,类似于包如何由一组类型组成。

Module: A new language feature introduced in Java 9 (similar to class, interface, package, etc.) that consists of a collection of packages, similar to how a package consists of a collection of types.

JAR :一种存档文件格式,它捆绑代码和资源,可以由JVM加载。

JAR: An archive file format that bundles code and resources and which can be loaded by the JVM.

更具体地说,模块是定义如下


为了提供重新负责任的配置和强大的封装以一种既可供开发人员使用并且可由现有工具链支持的方式,我们将模块视为一种基本的新型Java程序组件。 模块是一个命名的,自描述的代码和数据集合。它的代码被组织为一组包含类型的包,即Java类和接口;它的数据包括资源和其他类型的静态信息。

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.

...

模块的自我描述在其模块声明中表示,它是Java编程语言的一种新结构。

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

...

按照惯例,模块声明被编译到名为<$ c $的文件中c> module-info.class ,类似地放在类文件输出目录中。

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文件

A module can be compiled into a Jar file, in which case the Jar file is labelled a modular Jar file:


现有工具已经可以创建,操作和使用JAR文件,因此为了便于采用和迁移,我们定义了模块化JAR文件。模块化JAR文件在所有可能的方式都像普通的JAR文件,除了它的根目录中还包含 module-info.class 文件。

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之间的其他一些差异:

Some other differences between a module and a JAR:


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

  1. Modules can require other modules in order to allow accessing dependent classes by the requiring module. A Jar has no such dependency concept.

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

A module can decide which classes and interfaces to export to other modules that require it. A Jar has no such encapsulation mechanism.

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

A module can be compiled into a modular Jar file, but some modules (e.g. JDK modules) are compiled into another format called JMOD.

可以更改JAR的名称。只要JVM类加载器在类路径上找到所需的类(可以由单个JAR,多个JAR或目录或JAR之间的混合组成),JAR文件的名称就可以是任何名称。但是,模块的名称可以在其他模块的声明中明确引用,并且这样的名称定义它并且不能自由更改。

The name is of a JAR can be changed. As long as the JVM classloader finds the needed class on the classpath (which can be composed of a single JAR, multiple JARs, or a mix between directories or JARs), the name of the JAR file can be anything. However, the name of a module can be explicitly referenced in the declaration of other modules, and such the name defines it and cannot be changed freely.

这篇关于Java 9 - “模块”之间的区别是什么?和“JAR”文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆