Architecture & DesignDeveloping a Module with Java 9 in Eclipse IDE, Part 2

Developing a Module with Java 9 in Eclipse IDE, Part 2

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

In an earlier tutorial, “Developing a Module with Java 9 in Eclipse IDE, Part 1,” we introduced modules in Java 9. JSR 376: JavaTM Platform Module System provisions for a module system and Java 9 implements a module system. We defined a module and the associated directives of module, exports, and requires. We also discussed the objectives and benefits of the module system. In this continuation tutorial, we shall introduce using modules in Eclipse IDE. 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 (eclipse-jee-photon-M6-win32-x86_64.zip) is used in this tutorial.

Creating a Java Project

To create a Java project, select File>New>Java Project, as shown in Figure 1.

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

In the New Java Project window, specify the project details, as shown in Figure 2. Specify the Project name (HelloJigsaw) and select the checkbox Use default location. For JRE, select the Use an execution environment JRE radio button and select JavaSE-9. In Project layout, select Create separate folders for sources and class files and click the Configure default link.

New Java Project
Figure 2: New Java Project

We need to set the build path for the source and output folders. In Preferences (Filtered), only Java>Build Path is displayed, as shown in Figure 3. In Source and output folders, select the radio button Folders. Specify the Source folder name as src and Output folder name as modules.hello.jigsaw. Click Apply and Close.

Setting Build Path
Figure 3: Setting Build Path

Having configured the project details in Create a Java Project, click Next, as shown in Figure 4.

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

In Java Settings, the Source tab displays the folders src and modules, as shown in Figure 5. In Details, select the Create module-info.java file checkbox. The Default output folders field displays the default output folders. Click Finish.

Java Settings
Figure 5: Java Settings

Configuring a Module Declaration

In this section, we shall create a module declaration in the source code file module-info.java. The module declaration to add is as follows:

module hello.jigsaw {
}

The module does not declare any dependencies and does not export specific packages. By default, all packages in a module are exported. When the option to Create module-info.java file is selected, as shown in Figure 5, a New module-info.java dialog gets displayed (see Figure 6). Specify a module name which, by convention, usually starts with a lowercase letter. Specify a module name in the Module name field, hello.jigsaw as an example.

Specifying Module Name
Figure 6: Specifying Module Name

A new Java project gets created, including the module-info.java source file, as shown in Figure 7.

Java Project HelloJigsaw
Figure 7: Java Project HelloJigsaw

Adding the Main Class for the Module

In this section, we shall add the main class for the module. The main class is called Main and should be in a package by the same name as the module name, which is hello.jigsaw. The Main class to add is as follows; the class declares a main method that is invoked when the class is run and outputs a Hello Jigsaw message.

package hello.jigsaw;
public class Main {
   public static void main(String[] args) {
      System.out.println("Hello Jigsaw!");
   }
}

To add the Main class, right-click the HelloJigsaw project in Package Explorer and select New>Class. In the New Java Class window (see Figure 8), the Source folder should be pre-specified as HelloJigsaw/src. Specify the Package as hello.jigsaw, which is the same name as the module name. Specify the class name in the Name field as Main. Select the checkbox for adding the public static void main(String[] args) method, which makes the class a Java application, and click Finish.

Configuring Main Class
Figure 8: Configuring Main Class

The Main class gets added to the Java project HelloJigsaw, as shown in the Package Explorer in Figure 9.

Main Class added
Figure 9: Main Class added

Copy the code for the Main class, as listed earlier, to the Main class in Eclipse IDE, as shown in Figure 10.

Main Class with Source Code
Figure 10: Main Class with Source Code

Configuring VM Args for the Module Path

We have created a module declaration in the module-info.java file and the Main application file. How does the Main application find the module? We need to configure VM args for the Main application to include the module path. Right-click the HelloJigsaw project in Package Explorer and select Properties. In the Properties window, select Run/Debug Settings and select the Main class (see Figure 11). Click Edit…

Run/Debug Settings
Figure 11: Run/Debug Settings

In Edit Configuration (see Figure 12), the Main tab is selected by default, with the application name specified in the Name field as Main. The project is specified in the Project field as HelloJigsaw. The Main class is hello.jigsaw.Main.

Edit Configuration
Figure 12: Edit Configuration

Select the Arguments tab and specify the args in the VM arguments field, as shown in Figure 13, and as listed:

--module-path modules -m hello.jigsaw.Main

The –module-path arg specifies the module path as one or more directories with modules and the -m option specifies the module. Click OK.

VM Arguments
Figure 13: VM Arguments

In Run/Debug Settings, click Apply and Close, as shown in Figure 14.

Properties>Run/Debug Settings>Apply and Close
Figure 14: Properties>Run/Debug Settings>Apply and Close

Running the Java Module Application

In this section, we shall run the Java module application. Right-click the Main.java file in Package Explorer and select Run As>Java Application, as shown in Figure 15.

Main.java>Run As>Java Application
Figure 15: Main.java>Run As>Java Application

The Java module application runs and outputs a Hello Jigsaw message in the Console (see Figure 16).

Output from Module Application
Figure 16: Output from Module Application

Conclusion

In two tutorials, we introduced the new feature of modules in Java 9 and discussed creating a module in Eclipse IDE. A module is a set of named Java packages, resources, and native libraries; it provides modularization to the JDK. The module system is an implementation of the JSR 376: JavaTM Platform Module System.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories