- Create a project -
C:\..\modulesDemo
- Create a folder for java files under the project -
src\com.aj.modules
- Create a the java module file in the directory src\com.aj.modules -
module-info.java
module com.aj.modules {
}
- Create the folder for file to be included in the module -
src\com.aj.modules\com\aj\modules
- Create TestMain.java file
package com.aj.modules;
public class TestMain {
public static void main (String args[]) {
System.out.println("Hello modules from medium :)");
}
}
- Create a folder to store the module compiled files -
../modulesDemo/mods/com.aj.modules
- Before compilation folder structure
../modulesDemo
├── mods
└── com.aj.modules
└── src
└── com.aj.modules
├── module-info.java
└── com
└── aj
└── modules
└── TestMain.java
- Compile the module, navigate to the project folder i.e.
modulesDemo
, and compile the module using the command below.
javac -d mods/com.aj.modules src/com.aj.modules/module-info.java`
Command explaination-
<-d mods/com.aj.modules> - output directory
<src/com.aj.modules/module-info.java> - source directory
- Compile the module contents i.e. TestMain.java file.
javac -d mods/com.aj.modules src/com.aj.modules/com/aj/modules/TestMain.java
- After the compilation you will have a project structure as below
../modulesDemo
├── mods
└── com.aj.modules
├── module-info.class
└── com
└── aj
└── modules
└── TestMain.class
└── src
└── com.aj.modules
├── module-info.java
└── com
└── aj
└── modules
└── TestMain.java
- To run the TestMain class in the module, navigate to folder
/modulesDemo
, the run this command-
java --module-path mods -m com.aj.modules/com.aj.modules.TestMain
Command explaination-
java --module-path <path to module, compiled> -module <module name>/<package>.<main class>
- Here is the output-
C:\..\modulesDemo>java --module-path mods -m com.aj.modules/com.aj.modules.TestMain
Hello modules from medium :)