Java 9 introduces a new level of abstraction above packages, formally known as the Java Platform Module System (JPMS), or “Modules” for short. A Module is a group of closely related packages and resources along with a new module descriptor file. When we create a module, we organize the code internally in packages just like we previously did with any other project.
Problems of Current Java System
Java SE 8 or earlier systems have following problems in developing or delivering Java Based applications.
- As JDK is too big, it is a bit tough to scale down to small devices. Java SE 8 has introduced 3 types of compact profiles to solve this problem: compact1, compact2, and compact3. But it does not solve this problem. JAR files like rt.jar etc are too big to use in small devices and applications.
- As JDK is too big, our applications or devices are not able to support better Performance. There is no Strong Encapsulation in the current Java System because “public” access modifier is too open. Everyone can access it.
- As JDK, JRE is too big, it is hard to Test and Maintain applications.
- As the public is too open, They are not to avoid the accessing of some Internal Non-Critical APIs like sun.*, *.internal.* etc.
As User can access Internal APIs too, Security is also big issue. - Application is too big. Its a bit tough to support Less Coupling between components.
Advantages of Java SE 9 Module System
Java SE 9 Module System is going to provide the following benefits
- As Java SE 9 is going to divide JDK, JRE, JARs etc, into smaller modules, we can use whatever modules we want. So it is very easy to scale down the Java Application to Small devices.
- Ease of Testing and Maintainability.
- Supports better Performance.
- As public is not just public, it supports very Strong Encapsulation. (Don’t worry its a big concept. we will explore it with some useful examples soon).
- We cannot access Internal Non-Critical APIs anymore.
- Modules can hide unwanted and internal details very safely, we can get better Security.
- Application is too small because we can use only what ever modules we want.
- Its easy to support Less Coupling between components.
- Its easy to support Single Responsibility Principle (SRP).
Compare JDK 8 and JDK 9
We know what a JDK software contains. After installing JDK 8 software, we can see a couple of directories like bin, jre, lib etc in Java Home folder.
However, Oracle Corp has changed this folder structure a bit differently as shown below.JDK 8 Folder Structure:
JDK 9 Folder Structure:
Here JDK 9 does NOT contain JRE. In JDK 9, JRE is separated into a separate distribution folder. JDK 9 software contains a new folder “jmods”. It contains a set of Java 9 Modules as shown below.
In JDK 9, No rt.jar and No tools.jar
What is Java 9 Module?
A Module is a self-describing collection of Code, Data, and some Resources. It is a set of related Packages, Types (classes, abstract classes, interfaces etc) with Code & Data and Resources.
Each Module contains only a set of related code and data to support Single Responsibility (Functionality) Principle (SRP).
The main goal of Java 9 Module System is to support Modular Programming in Java.
We will discuss on “What is a Module Descriptor” and “How to develop Java Modules” in my coming posts.
As of now, Java 9 Module System has 95 modules in Early Access JDK. Oracle Corp has separated JDK jars and Java SE Specifications into two set of Modules.
- All JDK Modules starts with “jdk.*”
- All Java SE Specifications Modules starts with “java.*”
Java 9 Module System has a “java.base” Module. It’s known as Base Module. It’s an Independent module and does NOT dependent on any other modules. By default, all other Modules dependent on this module.
That’s why “java.base” Module is also known as The Mother of Java 9 Modules.
It’s default module for all JDK Modules and User-Defined Modules
.Some Module commands
In order to see which modules are available within Java, we can enter the following command:
java --list-modules
A list is shown with the available modules. Below is an extract from the list:
java.activation@9 java.base@9 java.compiler@9 ...
Module properties are located in a module-info.java file. In order to see the description of the module defined in this file, the following command can be used:
java --describe-module java.sql
This will output the following:
java.sql@9 exports java.sql exports javax.sql exports javax.transaction.xa requires java.logging transitive requires java.xml transitive requires java.base mandated uses java.sql.Driver
Create Your Own JRE
The directory structure of the JDK has changed a bit. A directory, jmods, is added. This directory contains a jmod file for each module. A jmod file is like a JAR file, but for modules. You can unzip the file and look at the contents. It contains the compiled classes for this module.
The size of the whole JDK is 482 MB on my laptop. When using jlink, we can create our own JRE with only the modules we are using in our application. The following command will create a JRE with only the java.base module.
jlink --module-path ../jmods --add-modules java.base --output d:\java\jre
The size of this runtime is only 36 MB. Pretty easy isn't it?
Example Explained
In this section, we will use a Hello Modules example. The following directory structure will be used:
mods src |_ com.behindjava.jpmshello - module-info.java |_ com |_mydeveloperplanet |_ jpmshello - HelloModules.java target
package com.behindjava.jpmshello; public class HelloModules { public static void main(String[] args) { System.out.println("Hello Modules!"); } }
The contents of the module-info.java is as follows.
module com.mydeveloperplanet.jpmshello { requires java.base; }
Compilation
The next step is to compile the classes. You can use the following option to compile
javac -d mods/com.mydeveloperplanet.jpmshello src/com.mydeveloperplanet.jpmshello/module-info.java src/com.mydeveloperplanet.jpmshello/com/mydeveloperplanet/jpmshello/HelloModules.java
No comments:
Post a Comment