I have already discuss few theoretical concepts about Java 9 Module System in my previous posts: “Java Module System”, “Module” and “Module Descriptor”. I’m going to discuss about “How to develop and test a Simple HelloWorld Java 9 Module by using Command (CMD) prompt” in this post.

In this series of “Java 9 Module System” posts, it is my third post. Before reading this post, please go through my previous post by clicking the following links to understand some basics about Java 9 Modules.

We have already discussed too much theory. Now, Let us start developing some simple Modules.

Post Brief Table of Content:

  • Introduction to “HelloWorld” Module
  • Steps to Develop a Java 9 Module
  • Develop “HelloWorld” Module
  • Develop “HelloWorldClient” Module
  • Set Java SE 9 Environment
  • Compile “HelloWorld” Module
  • Compile “HelloWorldClient” Module
  • Test “HelloWorldClient” Module

Introduction to “HelloWorld” Module

As a Developer, we first start with “HelloWorld” program to learn new Concept or Programming Language. In the same way, we start learning our Java 9 New concept “Modular Programming” with “HelloWorld” Module development.

In this post, we are going to develop the following two modules:

  • com.hello
  • com.hello.client

Our requirements for these two modules are:

  • com.hello module develops “HelloWorld” component.
  • com.hello module exports com.hello package to outside world.
  • com.hello.client module requires or imports com.hello module.
  • com.hello.client module uses “HelloWorld” component.

This complete problem statement is depicted as shown below:

java9_helloworld_module

Let us start developing our modules in the coming sections.

Steps to Develop a Java 9 Module

We will follow these steps one by one to develop and test our “HelloWorld” Module.

  • Create Module name folder, for example “com.hello”.
  • Create Module Packages, for example “com.hello”.
  • Create our Java component, for example “HelloWorld.java”.
  • Create Module Descriptor, for example “module-info.java”.
  • Define Module Description in Module Descriptor, for example “exports com.hello;” of “module-info.java” in “com.hello” module.
  • Create Module Jars if required.
  • Test our modules.

These steps are common for almost all modules development. Most of the commands used in this post are applicable to Windows OS. Few commands may differ for other OSs like Linux, Mac.

Develop “HelloWorld” Module

We first start with “HelloWorld” Module development. Please refer our Problem statement diagram for more details.

Create “HelloWorld” Module name folder: com.hello

Create “HelloWorld” Module package name folder: comhello

Please refer the following diagram for the above two steps:

java9_helloworld_module

Develop “HelloWorld.java” component under package name “com.hellocomhello”.

HelloWorld.java

Develop Module Descriptor at Module root folder “com.hello”.

module-info.java

If we observe this Module Descriptor, we can say that “com.hello” is exporting “com.hello” package to outside world so that our HelloWorldClient program can use it.

“com.hello” Module full tree structure as shown below:

java9_helloworld_module

Develop “HelloWorldClient” Module

Like “HelloWorld” Module, We need to follow the same steps to develop this module. Please refer our Problem statement diagram for more details.

Create “HelloWorldClient” Module name folder: com.hello.client

Create “HelloWorldClient” Module package name folder: comhelloclient

Develop “HelloWorldClient.java” component under package name “com.hellocomhelloclient”.

HelloWorldClient.java

Develop Module Descriptor at Module root folder “com.hello”.

module-info.java

If we observe above Module Descriptor, we can say that “com.hello.client” module is using types which are available in “com.hello” package name. It is not exporting anything to the outside world. So other modules cannot access types defined under “com.hello.client” package name.

Both “com.hello” and “com.hello.client” Modules full tree structure as shown below:

java9_helloworld_module

We have developed our two modules successfully. It’s time to compile them for testing purpose.

Set Java SE 9 Environment

Almost everyone knows, after installing Java SE 9 EA(Early Access) software we need to set two Environment variables.

As I’m using Java SE 8 for other projects, I’m setting these two variables only at Command Prompt. Please set these variables as System Variables.

java9_helloworld_module

NOTE:- If you are new to Java Tools (java,javac and jar) updates to support Java 9 Module System, please go through my post to learn them in-detail:

Java SE 9 Tools Changes (Link to be updated soon)

Compile “HelloWorld” Module

In this section, we will compile our “HelloWorld” first Module.

Please use the following javac command to compile this module:

Now, “com.hello” module output folder looks like as shown below:

java9_helloworld_module

As our “HelloWorldClient” Module uses this “HelloWorld” Module, we should have com.hello Module Jar file to refer it in Client module. Let us create this using the following jar command.

Now our “HelloWorld” Module archieved into a jar file: com.hello.jar which is located at mlib folder as shown below:

java9_helloworld_module

If we observe our “com.hello” module jar file content as shown above, we can see our Module Descriptor is compiled to “module-info.class” file.

Before moving to next step, please remove output directory.

Compile “HelloWorldClient” Module

In this section, we will compile our “HelloWorldClient” second Module. Java SE 9 “javac” command supports “module-path” option to refer other modules.

Please use the following javac command to compile this module:

We know our “com.hello.jar” file is located at “mlib” and “com.hello.client” is depending on “com.hello” module.

In order to refer “com.hello” module in “com.hello.client” compilation process, we should use “module-path” to refer “mlib” folder as shown above. Without this path, we cannot compile “com.hello.client” module components.

Test “HelloWorldClient” Module

Now, We have compiled both modules successfully. It’s time to test “com.hello.client” module component in this section.

“com.hello.client” module have a Java component: “HelloWorldClient.java” which contain a main() method so we can run this program as usual using “java” command.

Wow, Great News!.
We have successfully developed, compiled and tested “HelloWorld” module now.

That’s it all about “Develop and Test a Simple HelloWorld Java 9 Module by using Command(CMD) Prompt” topic. We will discuss some more concepts about Java SE 9 Modules Development in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

Thank you for reading my tutorials.

Happy Java SE 9 Learning!

By admin

Leave a Reply

%d bloggers like this: