编程语言
首页 > 编程语言> > java – 模块和JAR文件有什么区别?

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

作者:互联网

我从What’s New in Java9开始学习Java 9,讨论的热门话题之一是模块化JDK.

JAR是文件模块吗?

模块与JAR文件有何不同?

解决方法:

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

>模块可以要求其他模块,以允许需求模块访问依赖类. Jar没有这样的依赖概念.
>模块可以决定将哪些类和接口导出到需要它的其他模块. Jar没有这样的封装机制.
>模块可以编译成模块化的Jar文件,但是一些模块(例如JDK模块)被编译成另一种称为JMOD的格式.
>可以更改JAR的名称.只要JVM类加载器在类路径上找到所需的类(可以由单个JAR,多个JAR或目录或JAR之间的混合组成),JAR文件的名称就可以是任何名称.但是,模块的名称可以在其他模块的声明中显式引用,并且这样的名称定义它并且不能自由更改.

标签:java-module,java,jar,java-9
来源: https://codeday.me/bug/20190930/1836035.html