What is the difference between modules and JAR files?

后端 未结 3 1840
北海茫月
北海茫月 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:04

    Are JAR files Modules? How Module is different from JAR file?

    No, a Java Archive is not a Module.

    Just for an example, while classes of the same package could have been spread across JARs, the same package now can not be read from multiple modules.

    A JAR is a file format that enables you to bundle multiple files into a single archive file. Typically this contains the class files and auxiliary resources associated with applets and applications.

    on the other hand (I'd tried describing this here ~> java-module as well)

    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 also consists of the module declaration as specified with the help of module-info.java.

    Each module definition is either

    • A module artifact, i.e., a modular JAR file or a JMOD file containing a compiled module definition, or else

    • An exploded-module directory whose name is, by convention, the module's name and whose content is an "exploded" directory tree corresponding to a package hierarchy.

    As introduced with the module system, a modular image is composed of modules rather than JAR files. Modularity is foreseen for with dynamic configuration in terms of Modular WAR file as well.


    But for the ease of adoption of Modules a Modular JAR file, was introduced in JDK9, such that lets say for a module consisting of a module-info.java such that

    module com.foo.bar { }
    

    and other java classes as

    com/foo/bar/alpha/AlphaFactory.java
    com/foo/bar/alpha/Alpha.java
    

    Existing tools can already create, manipulate, and consume 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. A modular JAR file for the above com.foo.bar module, e.g., might have the content:

    META-INF/
    META-INF/MANIFEST.MF
    module-info.class
    com/foo/bar/alpha/AlphaFactory.class
    com/foo/bar/alpha/Alpha.class
    ...
    

    A modular JAR file can be used as a module, in which case its module-info.class file is taken to contain the module’s declaration. It can, alternatively, be placed on the ordinary class path, in which case its module-info.class file is ignored.

提交回复
热议问题