Working with multiple modules in eclipse – Java 9 Module system

/, Java 9/Working with multiple modules in eclipse – Java 9 Module system

Java 9 Module System :

One of the most exciting features of Java 9 is its support for developing modular Java applications. This guide shows how to work with Java 9 module system using eclipse step by step.

To understand Eclipse setup for java 9 See Eclipse setup for Java 9

Goals :

  1. Create EM (Employee Management) – Common module Project.
  2. Create source folder (module) with name com.em.common.
  3. Create a Employee model class in com.em.common.model package.
  4. Create file that exports com.em.common.model package.
  5. Create EM (Employee Management) – Service module Project.
  6. Create source folder (module) with name com.em.service.
  7. Create a EmployeeService service class in com.em.service.employee package.
  8. Create file that requires com.em.common module.
  9. Include EM-Common module project in module path to make it available for EM-Service module project.
  10. Finally write a Test class to test EmployeeService class. EmployeeSrvice provides Employee instance so that EmployeeService requires Employee.

Final structure of Module Projects :

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

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.

  1. As per our goal number 11, EmloyeeService provides Employee instance, so that Employee class should be imported in module project 2 EmployeeService class.
  2. But Employee class available in different project. In java previous versions to achieve this, you need to make a jar of module project 1 and then should add to the module project 2’s class path, then Employee class will be available for EmployeeService.
  3. 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.
  4. Follow this step 11 and step 12 and Step 13 to add module project 1 to add in module path of project 2.

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.

Testing Multi module Projects :

To understand how it works see the source code of both module projects. in EM-Common module. in EM-Common module. in EM-Service module. in EM-Service module.

Testing with

Conclusion :

In this guide we shown you how to work with multiple modules using java 9 Module system.

To understand Eclipse setup for java 9 See Eclipse setup for Java 9

Download Application – (11 KB)

Leave a Reply