Core Java

Java 9 Modules Tutorial

1. Introduction

In this example, I would like to show you how to get started with Java 9 modules. Oracle JSR site has details of the features.

Java 9 is a major release. At the time of writing this article, JDK 9 is currently available for early access download on the oracle site and is expected to be released on July 27, 2017. This document attempts to summarize details of JDK 9 modules and list some of the main features with this new release. Complete list of features in Java 9 can be viewed at the oracle site.

Here, I present some examples and details to get started along with some of the important features and commands of this useful feature added in Java 9.

2. Getting started with Java 9

To download the currently available early access JDK or JRE 9, visit http://jdk.java.net/9/.

Accept license
Accept license

As shown in the image above, at the site, accept the license agreement and proceed to the download section as shown below.

Download JDK
Download JDK

Please select the appropriate OS and appropriate option for 32/ 64 bits for the OS to download the JDK/ JRE. It is also recommended to download the documentation along with the JDK/ JRE installation.

Tip
You may refer to this article to get started with Java 9 by executing a simple Hello World program.

3. What is a Java 9 module

From Oracle site:

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.

Project Jigsaw includes JSR 376 that is planned to include Java Modular system in Java 9. From the oracle site, the below are the objectives of Project Jigsaw.

The primary goals of this Project are to:

  • Make the Java SE Platform, and the JDK, more easily scalable down to small computing devices;
  • Improve the security and maintainability of Java SE Platform Implementations in general, and the JDK in particular;
  • Enable improved application performance; and
  • Make it easier for developers to construct and maintain libraries and large applications, for both the Java SE and EE Platforms.

JDK binaries have been getting bigger. One of the biggest advantages of the new module system is that the JDK and JRE get divided into smaller modules that are easy to scale down if needed. Also, you would notice that the JDK folder does not contain the JRE folder in Java 9. A new folder jmods has been added in Java 9.

3.1 Java 9 JDK structure

JDK installation in Java 9 has changed, you can view the folder structure below.

JDK folder structure
JDK folder structure

You can view any of the existing module folders to see the module-info.java file.

module-info.java
module-info.java

Also, if you go to the java\lib folder in your JDK 9 installation, you will notice that tools.jar, rt.jar etc are missing. The lib folder looks like the below.

Java 9 lib folder
Java 9 lib folder

You can view the jmods folder to view the current modules. All jdk modules start with jdk*. The base module is the “java.base” module. This is also the biggest module under jmods folder.

jmods folder
jmods folder

3.2 Writing a new module

To get started with the creation of a new module, we must remember that a module has to have a unique name and needs to be defined in module-info.java. Also, the module-info.java class must be defined in a folder that has the same name as your module name. i.e. for the below module name, module-info.java must be placed in src\com.javacodegeeks.hello folder.

First we create a new module descriptor:

module-info.java

module com.javacodegeeks.hello
{

}

This is a simple and a basic module descriptor, this is the module used further in the example.

We can also have module descriptors with exports and requires clauses. Examples below:

module-info.java

module javacodegeeks.another.test
{
    exports com.javacodegeeks.test;

    requires com.javacodegeeks.test;

}

Here, requires clause specifies all the imports and exports clause specifies packages it exports to other modules. By default, all classes/ types inside a module are hidden to the outside world.

After creation of module descriptor, we will write a simple hello world class, this needs to go in the folder:

src\com.javacodegeeks.hello\com\javacodegeeks\hello\

HelloWorld.java

package com.javacodegeeks.hello
public class HelloWorld
{
    public static void main (String args [])
    {
        System.out.println (“Hello world”);
    }
}

To compile these via command line, run:

javac -d mods\com.javacodegeeks.hello com.javacodegeeks.hello\module-info.java com.javacodegeeks.hello\com\javacodegeeks\hello\HelloWorld.java

Now, use this module:

java --module-path mods -m com.javacodegeeks.hello/com.javacodegeeks.hello.HelloWorld

Output:

Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: mods\com.javacodegeeks.hello
Caused by: java.lang.module.InvalidModuleDescriptorException: HelloWorld.class found in top-level directory (unnamed package not allowed in module)

The error we’ve made is that we missed the package declaration in HelloWorld class. Let us add that in and repeat!

java --module-path mods -m com.javacodegeeks.hello/com.javacodegeeks.hello.HelloWorld

Output:

Hello world

This is a very simple test for the module features in java 9.

3.3 View existing modules

To see all existing modules you can type:

java --list-modules

The output in my version of JDK 9 early release download is:

java.activation@9-ea
java.base@9-ea
java.compiler@9-ea
java.corba@9-ea
java.datatransfer@9-ea
java.desktop@9-ea
java.instrument@9-ea
java.jnlp@9-ea
java.logging@9-ea
java.management@9-ea
java.management.rmi@9-ea
java.naming@9-ea
java.prefs@9-ea
java.rmi@9-ea
java.scripting@9-ea
java.se@9-ea
java.se.ee@9-ea
java.security.jgss@9-ea
java.security.sasl@9-ea
java.smartcardio@9-ea
java.sql@9-ea
java.sql.rowset@9-ea
java.transaction@9-ea
java.xml@9-ea
java.xml.bind@9-ea
java.xml.crypto@9-ea
java.xml.ws@9-ea
java.xml.ws.annotation@9-ea
javafx.base@9-ea
javafx.controls@9-ea
javafx.deploy@9-ea
javafx.fxml@9-ea
javafx.graphics@9-ea
javafx.media@9-ea
javafx.swing@9-ea
javafx.web@9-ea
jdk.accessibility@9-ea
jdk.attach@9-ea
jdk.charsets@9-ea
jdk.compiler@9-ea
jdk.crypto.cryptoki@9-ea
jdk.crypto.ec@9-ea
jdk.crypto.mscapi@9-ea
jdk.deploy@9-ea
jdk.deploy.controlpanel@9-ea
jdk.dynalink@9-ea
jdk.editpad@9-ea
jdk.hotspot.agent@9-ea
jdk.httpserver@9-ea
jdk.incubator.httpclient@9-ea
jdk.internal.ed@9-ea
jdk.internal.jvmstat@9-ea
jdk.internal.le@9-ea
jdk.internal.opt@9-ea
jdk.internal.vm.ci@9-ea
jdk.jartool@9-ea
jdk.javadoc@9-ea
jdk.javaws@9-ea
jdk.jcmd@9-ea
jdk.jconsole@9-ea
jdk.jdeps@9-ea
jdk.jdi@9-ea
jdk.jdwp.agent@9-ea
jdk.jfr@9-ea
jdk.jlink@9-ea
jdk.jshell@9-ea
jdk.jsobject@9-ea
jdk.jstatd@9-ea
jdk.localedata@9-ea
jdk.management@9-ea
jdk.management.agent@9-ea
jdk.naming.dns@9-ea
jdk.naming.rmi@9-ea
jdk.net@9-ea
jdk.pack@9-ea
jdk.packager@9-ea
jdk.packager.services@9-ea
jdk.plugin@9-ea
jdk.plugin.dom@9-ea
jdk.plugin.server@9-ea
jdk.policytool@9-ea
jdk.rmic@9-ea
jdk.scripting.nashorn@9-ea
jdk.scripting.nashorn.shell@9-ea
jdk.sctp@9-ea
jdk.security.auth@9-ea
jdk.security.jgss@9-ea
jdk.snmp@9-ea
jdk.unsupported@9-ea
jdk.xml.bind@9-ea
jdk.xml.dom@9-ea
jdk.xml.ws@9-ea
jdk.zipfs@9-ea
oracle.desktop@9-ea
oracle.net@9-ea

As you can see in the output above, JDK comes bundled with many modules. java.base module is the base module for all other modules.

3.4 Benefits of modules

Modules in java promise to make java light-weight, bring in reusability and make java easier to manage and debug. With Java becoming light-weight, it would allow Java to run on more devices.

Greater speed of development is expected with use of modules. Resources may be more effectively used in Java 9 with use of modules.

Another major feature of modules is encapsulation – modules would be given a safe place to run. We can specify explicit dependencies along with strong encapsulation.

4. Summary

We looked at some of the important reasons to use java 9 modules. Java 9 modules will allow for dynamic runtime configuration and easy encapsulation. It is a way out from current applications that use classpaths with many jars and often debugging is difficult.

5. References

http://www.journaldev.com/13106/javase9-module-system-part1
https://www.pluralsight.com/blog/software-development/java-9-new-features
https://www.romexsoft.com/blog/java-8-vs-java-9/
https://www.pluralsight.com/courses/java-9-modularity-first-look
Keynote session by Mark Reinhold

Sripriya Venkatesan

Sripriya is a Computer Science engineering graduate, she topped her graduation class and was a gold medalist. She has about 15 yrs of work experience, currently working as a technical architect/ technical manager for large scale enterprise applications, mainly around Java and database technologies; spanning different clients, geographies and domains. She has traveled to multiple countries and strives for work life balance. She is passionate about programming, design, architecture and enjoys working on new technologies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button