Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java 9 Module System: Enhancements, Link‑Time, and Custom Runtime Images

Java 9, a new kind of programming component called module has been introduced. A module is a self-describing collection of code and data and has a name to identify it.

Features

With the Modules component, following enhancements has been added in Java 9 −

Creating Module

Following the steps to create a module say com.tutorialspoint.greetings.

Step 1

Create a folder C:\>JAVA\src. Now create a folder com.tutorialspoint.greetings which is same as the name of module we're creating.

Step 2

Create module-info.java in C:\>JAVA\src\com.tutorialspoint.greetings folder with following code.

module-info.java

module com.tutorialspoint.greetings { }

module-info.java is the file which is used to create module. In this step we've created a module named com.tutorialspoint.greetings. By convention this file should reside in the folder whose name is same as module name.

Step 3

Add the source code in the module. Create Java9Tester.java in C:\>JAVA\src\com.tutorialspoint.greetings\com\ tutorialspoint\greetings folder with following code.

Java9Tester.java

package com.tutorialspoint.greetings;

public class Java9Tester {
   public static void main(String[] args) {
      System.out.println("Hello World!");
   }
}

By convention, the source code of a module to lie in same directory which is the name of the module.

Step 4

Create a folder C:\>JAVA\mods. Now create a folder com.tutorialspoint.greetings which is same as the name of module we've created. Now compile the module to mods directory.

C:/ > JAVA > javac -d mods/com.tutorialspoint.greetings 
   src/com.tutorialspoint.greetings/module-info.java 
   src/com.tutorialspoint.greetings/com/tutorialspoint/greetings/Java9Tester.java

Step 5

Let's run the module to see the result. Run the following command.

C:/>JAVA>java --module-path mods -m com.tutorialspoint.greetings/com.tutorialspoint.greetings.Java9Tester

Here module-path provides the module location as mods and -m signifies the main module.

Output

It will print the following output on console.

Hello World!

Java

  1. Mastering C# Using Statements: Imports, Aliases, and Static Directives
  2. Master Java Operators: Types, Syntax, & Practical Examples
  3. Master Java Input & Output: Print, Read, and Format Your Data
  4. Java Comments: Types, Usage, and Best Practices
  5. Mastering Java's super Keyword: Advanced Usage & Practical Examples
  6. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  7. Mastering Java Try‑with‑Resources: Automatic Resource Management Explained
  8. Java Annotations Explained: Types, Placement, and Practical Examples
  9. How to Install Oracle Java on Ubuntu Linux – Step‑by‑Step Guide
  10. Java 9 Module System: Enhancements, Link‑Time, and Custom Runtime Images