JDK 9 New Features




  1. Project Jigsaw: Module System Quick-Start Guide @

Why module?

The most important and drastic change in JDK 9 is certainly "module".

A module is a set of related packages that is designed for reuse. Before JDK 9, public classes are accessible to everyone and many APIs got exposed. Modules have concealed packages for internal use and exported packages for shared code with other modules. You can also specify which classes can be shared with which modules or you can set them to be accessible to any modules. In other words, you can fine tune the public access restriction to improve security with strong encapsulation and stability with reliable dependencies.

Example 1: Hello Module

Create a module called com.hello that prints "Hello, Module". The module consists of two sources files: a module declaration (or module descriptor) called module-info and a main class called Hello in package com.hellopack. By convention, the module directory name is the name of the module (i.e., com.hello); the package directory is com\hellopack.

I shall assume that our project base directory is c:\myProject, with a sub-directory src for the source files and sub-directory mods for the compiled module.

Create the src sub-directory and com.hello sub-sub-directory for the module com.hello.

Create module declaration file in c:\myProject\src\com.hello (top level directory of the module) as follows:

module com.hello {

Create the main class com.hellopack.Hello in c:\myProject\src\com.hello\com\hellopack\, as follows:

package com.hellopack;
public class Hello {
   public static void main(String[] args) {
      System.out.println("Hello, Module!");

We shall keep our compiled module in a sub-directory called mods (under c:\myProject). By convention, a module is kept in a directory the same as the module name, i.e., the module com.hello is kept in directory mods\com.hello.

Compile the module as follows, where -d option specifies the generated class file directory, which will be created automatically.

cd \myProject
javac -d mods/com.hello src/com.hello/ src/com.hello/com/hellopack/
   // Note: You can use either forward slash (Unix and Mac) or backward slash (Windows) as path separator in the javac command

The mods\com.hello\module-info.class and mods\com.hello\com\hellopack\Hello.class are created.

You can run the main class as follows, where --module-path (or -p) specifies the module path, -m specifies the main class (both -p and -m are new options in JDK9):

cd \myProject
java --module-path mods -m com.hello/com.hellopack.Hello
Hello, Module!
Example 2: Module Dependency

Let's create two modules: com.hello and, such that com.hello depends on

Create the module declaration for called c:\myProject\src\\ as follows:

module {
   exports com.worldpack;  // This package can be used by other modules

The module exports package com.worldpack, to be used by other modules.

Create a class in module package com.worldpack called c:\myProject\src\\com\worldpack\ as follows:

package com.worldpack;
public class World {
   public static String getWorld() {
      return "World";

Compile the module as follows:

javac -d mods/ src/ src/

Modify com.hello's module declaration src\com.hello\ as follows:

module com.hello {
   requires;   // Depends on module

Modify com.hello's to use's World class as follows:

package com.hellopack;
import com.worldpack.World;
public class Hello {
   public static void main(String[] args) {
      System.out.println("Hello, " + World.getWorld() + "!");

Compile the com.hello module as follows (where -p is the same as --module-path):

javac -p mods -d mods/com.hello src/com.hello/ src/com.hello/com/hellopack/

Run the modified Hello class:

java -p mods -m com.hello/com.hellopack.Hello
Hello, World!
Example 3: Packaging in Modular JAR Files

For deployment, we can package a module as a "Modular JAR file", which is a regular JAR file with a module descriptor module-info.class in its top-level directory.

We shall keep our modular JAR files in sub-directory mlib.

Create a sub-directory mlib under "c:\myProject".

Create modular JAR file for module as follows, where --create (or -c) for creating new JAR file, --file (or -f) specifies the name of the JAR file, -C to change the directory:

jar --create --file=mlib/ --module-version=1.0 -C mods/ .

Create modular JAR file for module com.hello as follows, where --main-class specifies the main entry class of the module:

jar --create --file=mlib/com.hello.jar --main-class=com.hellopack.Hello -C mods/com.hello .

You can run the modular JAR file directly as follows, where -p (or --module-path) specifies the module path, -m (or --module) specifies the module to be executed (both -p and -m are new options in JDK 9).

java -p mlib -m com.hello
Hello, World!