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

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

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

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文件的名称就可以是任何名称.但是,模块的名称可以在其他模块的声明中显式引用,并且这样的名称定义它并且不能自由更改.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值