Environment Setup
Using Eclipse Oxygen and JDK 9
Create Calculator Module
The steps below show how to create a new module
Create a new Java Project in Eclipse
Provide Project Name is com.calculator and select JRE
Create module descriptor: Right click on project and go to Configure > Create module-info.java
Input module descriptor name is com.operators
Structure of project after adding module descriptor
Code inside module descriptor
module com.operators {
exports com.operators;
}
Create com.operators package
Add Operators class as below:
package com.operators;
public class Operators {
public int add(int a, int b) {
return a + b;
}
public int sub(int a, int b) {
return a - b;
}
public int mul(int a, int b) {
return a * b;
}
}
Add Main class as below:
package com.operators;
public class Main {
public static void main(String[] args) {
}
}
Structure of project after adding Operators class
Run and export module
Right click on project and go to Run As > Java Application
Test Calculator Module
The steps below show how to test calculator module
Create a new Java Project in Eclipse
Provide Project Name is MyClientTest and select JRE
Add calculator module to test project: Right click on project and go to Build Path > Configure Build Path
In Project tab select Modulepath. Click Add button and select com.calculator module
After add calculator module
Create com.demo package in test project
Create module descriptor: Right click on project and go to Configure > Create module-info.java
Input module descriptor name is com.demo
Code inside module descriptor
module com.demo {
requires com.operators;
}
Create com.demo package and create Main class inside com.demo package
Run test project
package com.demo;
import com.operators.Operators;
public class Main {
public static void main(String[] args) {
Operators operators = new Operators();
System.out.println("Add: " + operators.add(10, 2));
System.out.println("Sub: " + operators.sub(10, 2));
System.out.println("Sub: " + operators.mul(10, 2));
}
}
Output
Add: 12
Sub: 8
Mul: 20