Java SE 9: Module and Module Descriptor Basics (Part-2) With Examples

I have already discuss about “Java 9 Module System” in high level in my previous post. I’m going to discuss about two more important concepts in this post: “Module” and “Module Descriptor” basics , which are required to start Java 9 Modules Development.

In this series of “Java Module System” posts, it is my second post. Before reading this post, please read my first post by clicking here “Introduction to Java 9 Module System” to understand about Jigsaw Project details.

Let us dive into this post now.

Post Brief Table of Content:

  • Introduction to Java 9 Module
  • Java 9 Module Basics
  • Java 9 Module Rules
  • Java 9 Module Descriptor
  • What is Module Meta Data?
  • Java 9 Module Descriptor Syntax
  • Are “module”, “requires”, and “exports” Keywords?
  • Java 8 Jars Vs Java 9 Modules
  • ClassPath Vs ModulePath
  • What is Modular JAR?

Introduction to Java 9 Module

A Java 9 Module is a named, self-describing collection of Code and Data. It’s Code is organized as a set of Packages containing types (classes, abstract classes, interfaces, enums etc). It’s Data includes resources and other kinds of static information.

Here Even though both Data and Resources are same, in few diagrams I have depicted them in separated boxes for clarity purpose.

NOTE:-
Module Descriptor (“module-info.java”) is a one of the Resources in Java 9 Module.

The two main goals of Java 9 Module System:

  • Reliable Configuration
  • Strong Encapsulation

As we are beginners to the Java 9 Module System, it’s really tough to understand these two terms. No worries, we will discuss these two terms in-depth after developing couple of Modules in my coming posts.

In Java 9 Module System, a Module is a First class citizen.

Java 9 Module Basics

We should remember the following important points about Java 9 Module:

  • Each module has a unique Name.
  • Each module has some description in a source file.
  • A Module description is expressed in a source file called “module-info.java”.
  • As “module-info.java” file describes a Module, it is also known as “Module Descriptor”.
  • A Module Descriptor is a Java file. It is not an XML, Text file or Properties file.
  • By convention, we should use same name “module-info.java” for Module Descriptor.
  • By convention, Module Descriptor file is placed in the top level directory of a Module.
  • Each Module can have any number of Packages and Types.
  • As of now, JDK 9 EA (Early Access) have 95 modules.
  • We can create our own modules.
  • One Module can dependent on any number of modules.
  • Each Module should have one and only one Module Descriptor (“module-info.java”).

Using Java SE 9, we develop Modules that means we can do Modular Programming in Java.

Java 9 Module Rules

We should follow these rules to develop Java 9 Modules:

    • Each module have a unique name

As Modules live in a global space in JVM, each module should have a unique name. Like package names and JAR file names, we can use Reverse Domain Name pattern to define a module name.

For example:-
We are going to develop modules for www.journaldev.com, then you can use “com.journaldev.mod1” as your first module name, then “com.journaldev.mod2” and so on.

    • Each module should have one and only one Module Descriptor

By convention, Java 9 Module system allows “module-info.java” as Module Descriptor. It should be placed at root folder of the Module.

For example:-
If We are going to develop “com.journaldev.mod1” module, then we should place our Module Descriptor under “com.journaldev.mod1” name.

  • A Module can have any number of packages.
  • One Module cannot have multiple sub-module code and description.

There are few more import Rules in Java 9 Module System. We should discuss them in my coming posts soon.

Java 9 Module Descriptor

In a Java 9 Module, Module Descriptor is a resource, which contains Module Meta Data. It is NOT an XML or a properties file, a plain Java file.

We must name this file as “module-info.java”. By convention, we mush place it at the root folder of the module. It is used to provide Module Description.

Like other Java source files, a Module file is compiled into “module-info.class” using javac command. We will see it in my next post.

A Module Descriptor is created using “module” keyword as shown below:

Example:-

It is a simple and minimal Module Descriptor example. Now Let us discuss what is a Module Meta Data?

What is Module Meta Data?

As we discussed in the previous section, we can use Module Descriptor to provide Module Meta Data. A Module has the following Meta Data:

  • A unique name.
  • exports clause.
  • requires clause.

Let us discuss these points in-depth with some useful examples here.

  • A Module can exports it’s packages to outside world so that other Modules can use them.

In Module Descriptor, we use “exports” clause to export a package(s) to outside world or to other Modules.

Example:-

NOTE:-
It is not mandatory to export all packages. It’s up-to the Module Owner to decide which one to export and which one not.

  • A Module can import or use other modules packages.

In Module Descriptor, we use “requires” clause to import other Module’s packages.

Example:-

In the previous point, “com.journaldev.mod1” has exported “com.journaldev.services” package so that here “com.journaldev.mod2” is importing and using them.

A Module can have more than this Meta Data. As a Beginner to Java 9 Module System, it is enough to start Modular Programming. We will discuss those few more Meta Data in my coming posts.

We will discuss a Module Descriptor Syntax in the next section.

Java 9 Module Descriptor Syntax

In brief, a Module Descriptor contains 3 main things:

  • A Module name.
  • exports clause.
  • requires clause.
  • java9_module_high_level-450x249

Important points to remember about a Module Descriptor

  • A Module Descriptor just have a Module name and nothing that means no exports clause and no requires clause.
  • A Module Descriptor can have only exports clause, no requires clause. That means it is exports it’s packages to other Modules and NOT depending on any other modules. It’s an Independent module.
  • A Module Descriptor can have both exports clause and requires clause. That means it is exports it’s packages to other Modules and also using other Module’s packages. It is depending on any other modules. It’s NOT an Independent module.
  • A Module Descriptor can have zero, one or more exports clause.
  • A Module Descriptor can have zero, one or more requires clause.

NOTE:- Here “requires” means importing a package from Other module into the current Module.

Example:-

Here “com.hello” Module is exporting it’s package “com.hello” to other Modules and importing or using “java.sql” Java 9 Module.

Are “module”, “requires”, and “exports” Keywords?

As we know in Java 9 Module System, We can use “module”, “requires”, and “exports” keywords to describe a Module Meta Data in a Module Descriptor.

However, all these 3 are not keywords. They are just “Contextual Keywords” only. That means they are keywords only with a Module Descriptor (“module-info.java”) file.

If we used them as Identifiers in the existing code base, Java Compiler or Run-time does NOT throw any error or Exceptions at compile-time and run-time.

We can use them as Identifiers without any issues as shown below:

Java 8 Jars Vs Java 9 Modules

As we know in Java 8 or earlier versions, we use create JARs (Of course WARS or EARs) to deploy them into servers or use them in other applications.

When we create Java 8 or earlier versions project in Eclipse IDE or any IDE, IDE adds lots of JDK JARs into our Project CLASSPATH as shown below:

java9_module_descriptor_syntax-434x450

When we create Java 9 Module project in Eclipse IDE or any IDE, IDE adds lots of JDK Modules into our Project MODULEPATH as shown below:

java9_module_descriptor_syntax-434x450

From Java 9 on-wards, We refer MODULEPATH instead of CLASSPATH. Of course, Java 9 supports both MODULEPATH and CLASSPATH.

We will see about “When we use MODULEPATH?” and “When we use CLASSPATH?” in my coming posts in depth.

In Java 9, we can develop both Public API and Private API in a Module as shown below.

java9_module_descriptor_syntax-434x450

ClassPath Vs ModulePath

As a Java Developer, we know what is CLASSPATH right. So far we are in CLASSPATH Hell or JAR Hell. From Java 9 on-wards, we are going to jump into another Hell: That is MODULEPATH Hell.

A ClassPath is a sequence of classes and packages (or JARs) which are user-defined classes and built-in classes. JVM or Java Compiler requires this to compile the application or classes.

A ModulePath is a sequence of Modules (which are provided in a Folder format or JAR format). If a Module is in folder format, that means its in Exploded Module format. If its in a JAR format, that jar is know as “Modular JAR“.

Don’t worry if you don’t understand these concepts. As we are going to learn new concept, it takes time to understand the new terminology. Move on to the next post.

I will explain all these concepts in-detail in my coming posts. ModulePath is one of the new concept or terminology in Java 9 Module System. We should understand it in-detail.

That’s it all about “Java 9 Module Basics” topic. We will start Java SE 9 Simple Modules Development in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

Thank you for reading my tutorials.

Happy Java SE 9 Learning!

By admin

Leave a Reply

%d bloggers like this: