What is the difference between modules and JAR files?

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 module different from a JAR file?

Answers


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: An archive file format that bundles code and resources and which can be loaded by the JVM.

More specifically, a module is 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.

A module can be compiled into a Jar file, in which case the Jar file is labelled a modular Jar file:

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.

Some other differences between a module and a 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.

  2. A module can decide which classes and interfaces to export to other modules that require it. A Jar has no such encapsulation mechanism.

  3. A module can be compiled into a modular Jar file, but some modules (e.g. JDK modules) are compiled into another format called JMOD.

  4. 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.


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.


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.


Need Your Help

JavaScript custom Event Listener

javascript event-listener

I was wondering if anyone can help me understand how exactly to create different Custom event listeners.

Methods that Clear the Thread.interrupt() flag

java multithreading interrupt

I have recently inherited a large Java Application that has almost no Thread safety in it. What I'm currently working on is getting all of the Threads to correctly handle being interrupted instead of