JavaData & JavaUsing Module Dependencies, Part 1

Using Module Dependencies, Part 1

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

A module could depend on other modules. If a module depends on another module, it is said to have a dependency on the other module. The Declaring Dependency on Other Modules section in the tutorial “Developing a Module with Java 9 in Eclipse IDE, Part 1” introduces module dependency. In a subsequent tutorial, “Developing a Module with Java 9 in Eclipse IDE, Part 2,” we discussed creating a module in Eclipse IDE. In a later tutorial, we discussed exporting a module to a JAR file and using the JAR file in an application. In this tutorial, consisting of two articles, we shall discuss module dependencies with an example in which one module has a dependency on another. Because we shall be using multiple modules, we would need to create multiple Java projects in Eclipse because Eclipse does not allow creating multiple modules in one project. This tutorial has the following sections:

Setting the Environment

Download and install an Eclipse IDE edition that supports Java 9. Eclipse IDE for Java EE Developers Version: Photon Release (4.8.0) is used in this tutorial.

Creating a Java Project for the Dependency Module

First, we shall create a Java project for the dependency module, the module the other module depends on. Select File>New>Java Project, as shown in Figure 1.

File>New>Java Project
Figure 1: File>New>Java Project

In New Java Project, specify a Project name (HelloJavaModule) and select the Use default location option for project, as shown in Figure 2. Set JRE to JavaSE-9. In Project layout, select the Create separate folders for sources and class files option and click the Configure default… link.

New Java Project
Figure 2: New Java Project

In Preferences for Java>Build Path, set the Source folder name to src and the Output folder name to modules/hellojavamodule, as shown in Figure 3. These are the default settings; therefore, nothing needs to be added. Click Apply and Close.

Preferences>Java>Build Path
Figure 3: Preferences>Java>Build Path

With the source and output folders configured, click Next, as shown in Figure 4.

New Java Project>Next
Figure 4: New Java Project>Next

In Java Settings, keep the default settings, which include the Create module-info.java file option selected, as shown in Figure 5. The module-info.java file is for the module declaration and a Java project cannot contain more than one. The Default Output folder should also be kept to the default setting. Click Finish.

Java Settings
Figure 5: Java Settings

The New module-info.java dialog displays the Module name (see Figure 6). By default, the module name is the same as the project name. As indicated in a message, a module name by convention usually starts with a lower-case letter, which is a best practice and not a requirement.

New module-info.java
Figure 6: New module-info.java

Modify the module name to start with a lower-case letter and click Create, as shown in Figure 7.

New module-info.java>Create
Figure 7: New module-info.java>Create

A new Java project gets created and added to the Package Explorer, as shown in Figure 8.

New Java Project Created
Figure 8: New Java Project Created

Select Project>Build Automatically for the project to build automatically, as shown in Figure 9.

Project>Build Automatically
Figure 9: Project>Build Automatically

Adding a Java Class to a Module

A module is a named set of Java packages, resources, and native libraries. Next, add a Java class (HelloJavaModule) that includes a method that outputs a message. To create a class, right-click the Java project in Package Explorer and select New>Class, as shown in Figure 10.

Package Explorer>HelloJavaModule>New>Class
Figure 10: Package Explorer>HelloJavaModule>New>Class

In the New Java Class dialog, the Source folder is set by default because we had previously chosen the Source and Output folders. Specify a Package (export.module), as shown in Figure 11. Specify class name (HelloJavaModule) in the Name field. Click Finish.

New Java Class>Finish
Figure 11: New Java Class>Finish

A new Java class gets created and added to the Package Explorer, as shown in Figure 12.

Java Class added
Figure 12: Java Class added

Copy the Java class HelloJavaClass from the following listing and paste it in the class source file.

package export.module;
public class HelloJavaModule {
   public static String name() {
      return "Exported Java Module";
   }
}

The HelloJavaModule class in Eclipse IDE is shown in Figure 13.

Adding HelloJavaModule Class listing
Figure 13: Adding HelloJavaModule Class listing

Adding the Module Declaration

The default module declaration created only declares a module. Add an exports directive to export the export.module package.

module helloJavaModule {
   exports export.module;
}

The module-info.java with the exports directive added is shown in Figure 14.

The module-info.java File
Figure 14: The module-info.java File

To save the source code files, select File>Save All, as shown in Figure 15.

File>Save All
Figure 15: File>Save All

Creating the Java Project for the Main Module

Having added the dependency project, next we shall add the Java project for the main module; this is the module that makes use of the other module. To create a project, select File>New>Java Project (see Figure 16).

File>New>Java Project
Figure 16: File>New>Java Project

In the New Java Project window, specify a Project name (MainModule) and, as for the dependency project, keep the Use default location option and the JRE setting to JavaSE-9 selected, as shown in Figure 17. Click the Configure default… link in Project layout to configure the Source and Output folders.

New Java Project>Main Module
Figure 17: New Java Project>Main Module

In the Java>Build Path Preferences window, keep the default value for the Source folder name and the Output folder name, as shown in Figure 18. Click Apply and Close.

Java>Build Path Preferences
Figure 18: Java>Build Path Preferences

Click Next in New Java Project, as shown in Figure 19.

New Java Project>Next
Figure 19: New Java Project>Next

In Java Settings, the Create module-info.java option is selected by default and a Default output folder is specified, as shown in Figure 20. Keeping the default Java settings, click Finish.

Java Settings
Figure 20: Java Settings

The New module-info.java dialog gets displayed with a Module name (see Figure 21). The default module name is the same as the project name and, as indicated in a message, the module name should start with a lower-case letter.

New module-info.java>Module name
Figure 21: New module-info.java>Module name

Modify the default module name to make it starts with a lower-case letter (mainModule) and click Create, as shown in Figure 22.

New module-info.java>Create
Figure 22: New module-info.java>Create

A Java project for the main module gets created and added to the Package Explorer, as shown in Figure 23.

Java Project MainModule
Figure 23: Java Project MainModule

Select Project>Build Automatically to make the Java project build automatically, as shown in Figure 24.

Selecting Project>Build Automatically
Figure 24: Selecting Project>Build Automatically

Adding the Main Class to the Module

Next, add the Java class for the main module. The Java class MainModule imports the Java class from the dependency module and invokes its name() method.

To create a new Java class, select File>New>Java Class as before. In the New Java Class window, specify a Package name (export.module) and specify a class name (MainModule) in the Name field, as shown in Figure 25. Select the option to create a stub for the public static void main(String[] args) method because the class is a Java application that we shall run. Click Finish.

New Java Class>Finish
Figure 25: New Java Class>Finish

A new Java class, MainModule, gets created and added to Package Explorer (see Figure 26).

Java Class MainModule added
Figure 26: Java Class MainModule added

The MainModule.java class is listed:

package main.module;
import export.module.HelloJavaModule;

public class MainModule {

   public static void main(String[] args) {
      System.out.format("Hello from %s!%n",
         HelloJavaModule.name());
   }

}

Copy the code for MainModule to the source file in Eclipse IDE, as shown in Figure 27.

MainModule.java with code added
Figure 27: MainModule.java with code added

Adding the Module Declaration for the Main Module

We need to modify the default module-info.java file for the module declaration. Add a requires directive for the dependency module helloJavaModule.

module mainModule {
   requires helloJavaModule;

}

The module-info.java code in the MainModule project is shown in Figure 28.

The main-info.java code for the MainModule project
Figure 28: The main-info.java code for the MainModule project

Select File>Save All, as shown in Figure 29, to save all the source code files.

Selecting File>Save All
Figure 29: Selecting File>Save All

Conclusion

In this article, we introduced module dependencies and created two projects—a main project and a dependency project—to demonstrate module dependencies. In a continuation article, we shall configure the two projects and run the main module Java application to invoke the dependency module Java class.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories