What is the difference between modules and JAR files?

后端 未结 3 1853
北海茫月
北海茫月 2020-12-14 16:57

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.

Are JAR files modules?

How is a

3条回答
  •  别那么骄傲
    2020-12-14 17:20

    Strictly speaking, a module is a run-time concept. As others have quoted from The State of the Module system:

    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.

    This is very similar to JARs, but...

    1. JARs have no meaningful representation at run time
    2. JARs are not "self-describing", which in this case means they do not have a name that the JVM cares about, can not express dependencies or define a proper API

    This leaves the question, where do modules come from? There are various ways but the most prominent one for developers is the modular JAR. A modular JAR is just like a plain JAR, but it contains a module descriptor, a file module-info.class that was compiled from a module-info.java. It is that file that defines a module's name, dependencies, and APIs.

    So there is a strong connection between JARs and modules: JARs are the containers from which the module system creates modules and (at the moment) each JAR can only contain a single module. It is important to note that even on Java 9 JARs do not have to be modular - plain JARs are totally fine.

提交回复
热议问题