
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to create a module in Java 9?
The module is a package of code and data. The module's code has organized into multiple packages and each package contains java classes and interfaces. The module's data includes resource files and other static information. An important feature of the module is that it contains "module-info.class" file that describes the module in the root directory of its artifacts. The artifact format can be a traditional JAR file or a JMOD file. This file is compiled from the source code file module-info.java in the root directory.
We can declare a module in module-info.java file with the new keyword module, the basic module declaration for a module com.company.mymodule is given below.
module com.tutorialspoint.mymodule { }
Steps to create a module:
First Step:
Create a folder C:\JAVA\src and then create a folder com.tutorialspoint.greetings with the same name as the module.
Second Step:
Create a module-info.java file in the C:\JAVA\src\com.tutorialspoint.greetings directory with the following code.
module com.tutorialspoint.greetings { }
Third Step:
Add a source code file to the module, and create a file JavaTest.java in the directory C:\JAVA\src\com.tutorialspoint.greetings\com\tutorialspoint\greetings, the code is as follows:
package com.tutorialspoint.greetings; public class JavaTest { public static void main(String args[]) { System.out.println("Hello Tutorialspoint!"); } }
Fourth Step:
Create a folder C:\JAVA\mods, and then create a com.tutorialspoint.greetings folder in this directory, and compile the module to this directory.
C:\JAVA>javac -d mods/com.tutorialspoint.greetings src/com.tutorialspoint.greetings/module-info.java C:\JAVA>javac -d mods/com.tutorialspoint.greetings src/com.tutorialspoint.greetings/com/tutorialspoint/greetings/JavaTest.java
Fifth Step:
Execute the module and see the output
C:\JAVA>java --module-path mods -m com.tutorialspoint.greetings/com.tutorialspoint.greetings.JavaTest Hello Tutorialspoint!
In the above, module-path specifies the path where the module is located and -m specifies the main module.
- Related Articles
- Importance of Module Descriptor in a Module in Java 9?
- What is Module System in Java 9?
- How can we display all module names in Java 9?
- How can we modify an existing module in Java 9?
- When to use the ServiceLoader class in a module in Java 9?
- How to create a thread in JShell in Java 9?
- What are the characteristics of a module in Java 9?
- What are the benefits of a module in Java 9?
- How to create a process using ProcessBuilder in Java 9?
- What is an unnamed module in Java 9?
- How to create static VarHandle in Java 9?
- What are the different module types in Java 9?
- How to create JShell instance programmatically in Java 9?
- How to create Html5 compliant Javadoc in Java 9?
- What are the components in a module-info file in Java 9?
