Home core java Working with multiple modules in eclipse - Java 9 Modules

Working with multiple modules in eclipse – Java 9 Modules

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 :

  1. Create EM–Common module Project, this is like common module to reuse in other modules in Employee Management 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 module-info.java file that exports com.em.common.model package.

Create Module 2 EM–Service :

  1. Create EM–Service module Project.
  2. Create source folder (module) with name com.em.service.
  3. Create a EmployeeService service class in com.em.service.employee package.
  4. Create module-info.java file that requires com.em.common module.
  5. Include EM-Common module project in module path to make it available for EM-Service module project.
  6. 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 :

java 9 multiple modules project structure in eclipse

2. Step by Step guide to achieve above goals :

Step 1 : In eclipse select File -> New -> Java Project.

Create java project in eclipse java 9

Step 2 : In project creation window enter Project name EM-Common -> select JRE version as Java SE 9.

Finish project creation in eclipse java 9

Step 3 : Add new source folder to the project. Remove default src source folder.

Add source folder in eclipse

Step 4 : Enter folder name and click on finish.

Finish source folder in eclipse

Step 5 : Right click on source folder -> select New -> select Class.

Create new class in eclipse

Step 6 : Enter package name -> enter Java class name -> Finsh.

Save class in eclipse

Step 7 : Right click on project -> Select configure -> Select module-info.java.

Create module-info in eclipse

Step 8 : Enter the module name -> then Finish step.

Save module-info in eclipse java 9

Step 9 : Your module project1 structure looks like below.

Module project 1 structure in eclipse java 9

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.

multiple module structure in eclipse java 9

  1. As per our goal number 6 in EM-Service module, EmployeeService class should use Employee class.
  2. 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.
  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 EM-Common to add in module path of project.

Step 11 : Right click on module project 2 -> Select Build Path -> Select Configure Build Path.

Configure build path in eclipse java 9

Step 12 : Select projects tab -> Select module path -> Select Add button.

Add module path in eclipse java 9

Step 13 : In Required Project selection window select module project 1 -> Click OK.

Add module to module path in eclipse java 9

Step 14 : Check final structure of the module projects.

Multi module project in eclipse

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)

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories