模块和JAR文件之间有什么区别? [英] What is the difference between modules and JAR files?

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

问题描述

我正在从 Java9的新功能了解Java 9讨论中的热门话题之一是模块化JDK .

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

JAR文件模块吗?

模块与JAR文件有何不同?

How is a module different from a JAR file?

推荐答案

模块:Java 9中引入了新的语言功能(类似于类,接口,包,等等)组成的包,类似于包由类型的集合组成的方式.

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.

...

按照惯例,模块声明被编译成名为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. 模块可以要求其他模块,以便允许Requirement模块访问相关类. 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 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.

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

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