YouTip LogoYouTip

Java9 Module System

# Java 9 Module System [![Image 3: Java 9 New Features](#) Java 9 New Features](#) One of the biggest changes in Java 9 is the introduction of the module system (Project Jigsaw). A module is an encapsulation of code and data. The code of a module is organized into multiple packages, each containing Java classes and interfaces; the data of a module includes resource files and other static information. An important characteristic of a Java 9 module is that it contains a `module-info.class` file in the root directory of its artifact, which describes the module. The artifact format can be either the traditional JAR file or the new JMOD file introduced in Java 9. This file is compiled from the source file `module-info.java` in the root directory. This module declaration file can describe the various characteristics of the module. In the `module-info.java` file, we can declare a module using the new keyword `module`, as shown below. Here is the most basic module declaration for a module named `com.mycompany.mymodule`. module com.tutorial.mymodule {} * * * ## Creating a Module Next, we will create a module named `com.tutorial.greetings`. **Step 1** Create a folder `C:>JAVAsrc`, and then create a folder with the same name as the module, `com.tutorial.greetings`, under that directory. **Step 2** Create a `module-info.java` file in the directory `C:>JAVAsrccom.tutorial.greetings` with the following code: module com.tutorial.greetings { } `module-info.java` is used to create a module. In this step, we have created the `com.tutorial.greetings` module. **Step 3** Add source code files to the module. Create a file `Java9Tester.java` in the directory `C:>JAVAsrccom.tutorial.greetingscomtutorialgreetings` with the following code: package com.tutorial.greetings;public class Java9Tester { public static void main(String[] args) { System.out.println("Hello World!"); }} **Step 4** Create a folder `C:>JAVAmods`, and then create a `com.tutorial.greetings` folder under that directory. Compile the module into this directory: C:/>JAVA> javac -d mods/com.tutorial.greetings src/com.tutorial.greetings/module-info.java src/com.tutorial.greetings/com/tutorial/greetings/Java9Tester.java **Step 5** Execute the module and view the output: C:/>JAVA> java --module-path mods -m com.tutorial.greetings/com.tutorial.greetings.Java9TesterHello World! **module-path** specifies the path where the modules are located. **-m** specifies the main module. [![Image 4: Java 9 New Features](#) Java 9 New Features](#)
← Java9 Improved JavadocsBootstrap4 Media Objects β†’