Wednesday, February 17, 2021

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

In an earlier post, "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 post, we shall introduce using modules in Eclipse IDE. This post 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.

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.

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.

Figure 3: Setting Build Path

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

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.

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.

Figure 6: Specifying Module Name

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

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.

Figure 8: Configuring Main Class

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

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.

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…

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.

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.

Figure 13: VM Arguments

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

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.

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).

Figure 16: Output from Module Application

Source: developer.com

Related Posts

0 comments:

Post a Comment