Java SE 9: Develop and Test Simple Module With CMD Prompt (Part-3)

Filed Under: Java

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:

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


mkdir com.hello

Create “HelloWorld” Module package name folder: com\hello


mkdir com.hello\com\hello

Please refer the following diagram for the above two steps:

Develop “HelloWorld.java” component under package name “com.hello\com\hello”.

HelloWorld.java


package com.hello;

public class HelloWorld {

  public String sayHelloWorld() {

      return "Hello World!";
  }

}

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

module-info.java


module com.hello {

  exports com.hello;
 
}

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:

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


mkdir com.hello.client

Create “HelloWorldClient” Module package name folder: com\hello\client


mkdir com.hello\com\hello\client

Develop “HelloWorldClient.java” component under package name “com.hello\com\hello\client”.

HelloWorldClient.java


package com.hello.client;

import com.hello.HelloWorld;

public class HelloWorldClient {

  public static void main (String arg[]) {

    HelloWorld hello = new HelloWorld();
    System.out.println(hello.sayHelloWorld());
	
  }

}

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

module-info.java


module com.hello.client {

  requires com.hello;
  
}

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:

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.

  • JAVA_HOME
  • PATH

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.

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:


F:\Java9ModuleExamples>javac -d output com.hello\com\hello\HelloWorld.java com.hello\module-info.java

Or

F:\Java9ModuleExamples>javac -d output com.hello\com\hello\HelloWorld.java 

F:\Java9ModuleExamples>javac -d output com.hello\module-info.java

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

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.


jar -c -f mlib\com.hello.jar -C output .

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

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.


F:\Java9ModuleExamples>rmdir /s output
output, Are you sure (Y/N)? Y

F:\Java9ModuleExamples>mkdir output

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:


F:\Java9ModuleExamples>javac --module-path mlib -d output com.hello.client\module-info.java

F:\Java9ModuleExamples>javac --module-path mlib -d output com.hello.client\com\hello\client\HelloWorldClient.java

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.


F:\Java9ModuleExamples>java --module-path mlib -m com.hello.client
Hello World!

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!

Comments

  1. felix says:

    CORRECTİON FOR JAR CREATE :

    C:\Users\pc\Desktop\JAVA DENEMELER\Java9ModuleExamples\output>jar cf “C:
    \Users\pc\Desktop\JAVA DENEMELER\Java9ModuleExamples\mlib”\com.hello.jar

    IT S İMPORTANT TO CURRENT DİRECTORY !!!
    com\hello\HelloWorld.class

    C:\Users\pc\Desktop\JAVA DENEMELER\Java9ModuleExamples\output>

  2. felix says:

    C:\Users\pc\Desktop\JAVA DENEMELER\Java9ModuleExamples>mkdir mlib

    C:\Users\pc\Desktop\JAVA DENEMELER\Java9ModuleExamples>jar cf mlib\com.h
    ello.jar “C:\Users\pc\Desktop\JAVA DENEMELER\Java9ModuleExamples\output\
    com\hello\HelloWorld.class

    C:\Users\pc\Desktop\JAVA DENEMELER\Java9ModuleExamples>

    FOR CREATE A JAR FİLE ANOTHER WAY :=))))))

  3. Krunal says:

    Hi,
    I am not able to run command

    jar -c -f mlib\com.hello.jar -C output

    It give me error parsing…

    Can you please guide me

    1. Vinay Magar says:

      It’s not below:
      jar -c -f mlib\com.hello.jar -C output

      It’s like below:
      jar -c -f mlib\com.hello.jar -C output .

      You were not giving the directory location

  4. Jerin Dev says:

    Got the similar issue: Error occurred during initialization of boot layer
    java.lang.module.FindException: Module com.hello.client not found

    D:\PROJECTS\jdk-9-samples>javac –module-path mlib -d output com.hello.client\module-info.java

    D:\PROJECTS\jdk-9-samples>javac –module-path mlib -d output com.hello.client\com\hello\client\HelloWorldClient.java

    D:\PROJECTS\jdk-9-samples>java –module-path mlib -m com.hello.client
    Error occurred during initialization of boot layer
    java.lang.module.FindException: Module com.hello.client not found

    D:\PROJECTS\jdk-9-samples>java -version
    java version “9”
    Java(TM) SE Runtime Environment (build 9+181)
    Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

    1. Darpan Kumar says:

      Try running——->
      java –module-path mlib;output -m com.hello.client/com.hello.client.HelloWorldClient

      1. felix says:

        it works thanks ; ))

      2. Zahra says:

        java -–module-path mlib/com.hello.jar;output –module com.hello.client/com.hello.client.HelloWorldClient

      3. Tanoy says:

        javac -d out com.hello/module-info.java com.hello/com/hello/HelloWorld.java
        jar –create –file mlib/com.hello.jar -C ./out .
        javac –module-path mlib/ -d out com.hello.client/module-info.java com.hello.client/com/hello/client/HelloWorldClient.java
        jar –create –file mlib/com.hello.client.jar –main-class=com.hello.client.HelloWorldClient -C ./out .
        java –module-path mlib/ –module com.hello.client

    2. JaccoC says:

      Above statement by Darpan Kumar should be :
      java –module-path “mlib;output” -m com.hello.client/com.hello.client.HelloWorldClient

  5. Dipannita Basu says:

    Getting the error:
    Error occurred during initialization of boot layer java.lang.module.FindException: Module com.hello.client not found

    while testing.
    I followed the steps and only getting error at this step. I have not deleted anything.

    Please help.

  6. Kenneth Wallis says:

    Please note a typo in the definition of the HelloWorldClient module’s folder structures. (ref: “mkdir com.hello\com\hello\client” and subsequent reference too) You’ve omitted the “client” portion of the names.
    Corrections should read “com.hello.client\com\hello\client”

  7. Damayanti says:

    Hi ,
    I am not able to Test “HelloWorldClient” Module. I am getting following error

    D:\java9>java –module-path mlib -m com.hello.client
    Error occurred during initialization of VM
    java.lang.module.ResolutionException: Module com.hello.client not found
    at java.lang.module.Resolver.fail(java.base@9-ea/Resolver.java:850)
    at java.lang.module.Resolver.resolveRequires(java.base@9-ea/Resolver.java:103)
    at java.lang.module.Configuration.resolveRequiresAndUses(java.base@9-ea/Configuration.java:311)
    at java.lang.module.ModuleDescriptor$1.resolveRequiresAndUses(java.base@9-ea/ModuleDescriptor.java:2522)
    at jdk.internal.module.ModuleBootstrap.boot(java.base@9-ea/ModuleBootstrap.java:278)
    at java.lang.System.initPhase2(java.base@9-ea/System.java:1928)

    For your information java version is given below:
    D:\java9>java -version
    java version “9-ea”
    Java(TM) SE Runtime Environment (build 9-ea+155)
    Java HotSpot(TM) 64-Bit Server VM (build 9-ea+155, mixed mode)

    can you tell how to resolve this ?

    1. Rambabu says:

      First of all, Thanks for reading my posts.
      Please follow all the steps clearly. Have your removed some unwanted stuff before doing next step?

      Ram

    2. marvin says:

      I’ve gotten the same error 🙁

  8. Srinivas J says:

    What is the difference in doing the above program in above fashion vs jar version

    1. Rambabu says:

      There is no difference. Just noted two different approaches. Both works in same way.
      When we have a jar, we can use it in other modules in IDEs.
      As its initial stages of Java 9, we don’t have much IDE support. That’s why I have give this CMD approach.
      Go for IDE post for more information where we don’t do these steps.

      Ram

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages