In this article we will see how to create java 9 module in eclipse, also will see how to work with multiple modules in eclipse by creating a example project in eclipse.
1. Goals :
Create Module 1 EM–Common :
- Create EM–Common module Project, this is like common module to reuse in other modules in Employee Management project.
- Create source folder (module) with name com.em.common .
- Create a Employee model class in com.em.common.model package.
- Create module-info.java file that exports com.em.common.model package.
Create Module 2 EM–Service :
- Create EM–Service module Project.
- Create source folder (module) with name com.em.service.
- Create a EmployeeService service class in com.em.service.employee package.
- Create module-info.java file that requires com.em.common module.
- Include EM-Common module project in module path to make it available for EM-Service module project.
- Finally write a Test class to test EmployeeService class in EM-Service module project. EmployeeService should use Employee class that belongs to Module EM-Common.
Final structure of Module Projects :
2. Step by Step guide to achieve above goals :
Step 1 : In eclipse select File -> New -> Java Project.
Step 2 : In project creation window enter Project name EM-Common -> select JRE version as Java SE 9.
Step 3 : Add new source folder to the project. Remove default src source folder.
Step 4 : Enter folder name and click on finish.
Step 5 : Right click on source folder -> select New -> select Class.
Step 6 : Enter package name -> enter Java class name -> Finsh.
Step 7 : Right click on project -> Select configure -> Select module-info.java.
Step 8 : Enter the module name -> then Finish step.
Step 9 : Your module project1 structure looks like below.
Step 10 : Now you need to create module project 2, to do that repeat step 1 to step 9. Then your 2 projects looks like below. Forget about that red mark, its compiler issue, if follow this steps it will be resolved when you reach end of last step.
- As per our goal number 6 in EM-Service module, EmployeeService class should use Employee class.
- But Employee class available in different project. In java previous versions, to achieve this you need to make a jar of that EM-Common project and then should add to the EM-Service’s class path, then Employee class will be available for EmployeeService.
- Java 9 supports module system, that will allow you to add the collection of packages (Module), make them available to different module using module path.
- Follow this step 11 and step 12 and Step 13 to add EM-Common to add in module path of project.
Step 11 : Right click on module project 2 -> Select Build Path -> Select Configure Build Path.
Step 12 : Select projects tab -> Select module path -> Select Add button.
Step 13 : In Required Project selection window select module project 1 -> Click OK.
Step 14 : Check final structure of the module projects.
3. Testing Java 9 module Projects :
To understand how it works see the source code of both module projects.
3.1. Employee.java in EM-Common module.
package com.em.common.model; public class Employee { private Long id; private String firstName; private String lasttName; private String designation; public Long getId() { return id; } public void setId(Long id) { this.id = id; } // Other setters and getters }
3.2. module-info.java in EM-Common module.
module com.em.common { exports com.em.common.model; }
3.3. EmployeeService.java in EM-Service module.
package com.em.service.employee; import com.em.common.model.Employee; public class EmployeeService { public Employee getEmployee() { Employee employee = new Employee(); employee.setFirstName("Peter"); employee.setLasttName("Milanovich"); employee.setDesignation("Scrum Master"); return employee; } }
3.4. module-info.java in EM-Service module.
module com.em.service { requires com.em.common; }
3.5. Testing with EmployeeServiceTest.java :
package com.em.service.employee; import com.em.common.model.Employee; public class EmployeeServiceTest { public static void main(String[] args) { Employee e = new EmployeeService().getEmployee(); System.out.println(e.getFirstName()); System.out.println(e.getLasttName()); System.out.println(e.getDesignation()); //Output: //Peter //Milanovich //Scrum Master } }
Conclusion :
In this article we have covered how to create java module in eclipse and how to work with multiple modules in eclipse. To work with modules in eclipse setting up Java 9 in eclipse is prerequisite. Download Application – Java_9_Module_App.zip (11 KB)
[…] Check out How to Create Java 9 Modules in Eclipse […]