Java SE 9: Develop and Test HelloWorld Module With Eclipse And IntelliJ IDEA IDEs (Part-4)

Filed Under: Java

I have already discuss about “Java Module System” Basics in my previous posts. I’m going to discuss about “How to develop and test a Simple HelloWorld Java 9 Module by using the two popular IDEs: Eclipse and IntelliJ IDEA” in this post.

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

Now, Let us start developing HelloWorld and it’s clients Modules.

Post Brief Table of Content:

  • Dev Environment Setup for Eclipse IDE and JDK 9
  • Develop “HelloWorld” Module With Eclipse IDE
  • Develop and Test “HelloWorldClient” Module With Eclipse IDE
  • Develop “HelloWorld” Module With IntelliJ IDEA IDE
  • Develop and Test “HelloWorldClient” Module With IntelliJ IDEA IDE

I have already discussed about “HelloWorld” and “HelloWorldClient” Modules requirements in my third post’s first section. Please read HelloWorld Module With CMD Prompt once before going through the next sections.

Dev Environment Setup for Eclipse IDE and JDK 9

I’m going to use “Eclipse Oxygen M4” with “JDK 1.9 EA (Early Access)” to develop our application. Please download and setup Eclipse and JDK Softwares.

Add JDK 9 Path to “Eclipse Installed JREs” as shown below:

Develop “HelloWorld” Module With Eclipse IDE

  • Create a Java project “com.hello” by selecting “JDK 9 EA” option as shown below:
  • Create our required Source folder “com.hello” by selection “module-info.java” option as shown below. This source folder is the root folder of our HelloWorld Module.
  • This step creates default module-info.java file at Module root folder as shown below:

  • Create a package “com.hello”. Then develop our HelloWorld.java component.
  • 
    package com.hello;
    
    public class HelloWorld {
    
      public String sayHelloWorld() {
    
          return "Hello World!";
      }
    
    }
    
  • Export package to other Modules in module-info.java as shown below:
  • 
    module com.hello {
    
      exports com.hello;
     
    }
    

    That’s it about “HelloWorld” Module development. Final “HelloWorld” Module project structure looks like below:

    Let us move to “HelloWorldClient” Module development in next section.

Develop and Test “HelloWorldClient” Module With Eclipse IDE

Fallow Same “HelloWorld” Module steps to “HelloWorldClient” Module.

  • Create “com.hello.client” project.
  • Create Source folder “com.hello.client” with “module-info.java”.
  • Create “HelloWorldClient.java” component under “com.hello.client” package.
  • 
    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());
    	
      }
    
    }
    
  • Update “module-info.java” to import “com.hello” to use Hello.java in “HelloWorldClient.java” component.
  • 
    module com.hello.client {
    
      requires com.hello;
      
    }
    
  • Now if you see the “module-info.java” in Eclipse IDE, it shows an error as shown below.
  • As “com.hello” Module is not in “com.hello.client” build path, it is showing that error message.

    Let us add “com.hello” project to “com.hello.client” build path as shown below:

    It resolves Module dependency and also solves import statements issues.

  • Final “com.hello.client” Module project structure looks like below.

Test “HelloWorld” Module With Eclipse IDE
To test our Modules, right click on “HelloWorldClient.java” and select “Run As” >> “Java application” to see the following output:


Hello World!

It is similar to our “CMP Prompt” example. There is no difference between Part-3 and this example, except Commands and IDEs.

Develop “HelloWorld” Module With IntelliJ IDEA IDE

To develop and test “HelloWorld” Module, I’m going to use latest version of IntelliJ IDEA: IDEA 2017.1 EAP.

You can download this software from this link:
https://confluence.jetbrains.com/display/IDEADEV/IDEA+2017.1+EAP

  • Open IntelliJ IDEA and click on “Create New Project” option as shown below
  • Select “Java” from left side List and add your JDK 9 as shown below
  • Click on “Next” and “Next” buttons.

  • Provide Project name: “helloworld”
  • Click on “Finish” button. This step creates our base project.

  • Create “com.hello” Module under “helloworld” Project
  • Create “module-info.java” file.
  • Then create a package “com.hello” and Create “HelloWorld.java” file.

    NOTE:- Please copy content from Eclipse IDE examples to “module-info.java” and “HelloWorld.java” files.

Develop and Test “HelloWorldClient” Module With IntelliJ IDEA IDE

  • In the same way, Create “com.hello.client” Module under “helloworld” Project
  • Create and copy content from from Eclipse IDE examples to “module-info.java” and “HelloWorldClient.java” files.

    Now, we have developed both Modules successfully. However, if you observe in the bottom left corner of IDE, we will see one error (Similar to Eclipse IDE error).

    To resolve this issue, we need to add “com.hello” Module as dependency.

  • Add “com.hello” Module as dependency to “com.hello.cleint” Module as shown below.
  • Click on “Open Module Settings” option as shown below:

    Add “com.hello” Module.

    Click on “OK” button. It resolves all issues.

  • Right click on “HelloWorldClient.java” and run the program.
  • You can see the output and complete Project and Modules structure in the below diagram

Wow, Congratulations!

We are successfully developed and tested our Favourite “HelloWorld” Module example using two popular IDEs.

That’s it all about “Developing and Testing a Simple HelloWorld Java 9 Module by using Eclipse and IntelliJ IDEA IDEs”. We will discuss some more new and complex 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. Cuong Ngo says:

    I’ve haven’t got the purpose of module-info.java yet, because even if I don’t have those two files for helloworld and helloworldclient, after I add helloworld as an dependency for helloworldclient, then helloworldclient can combine and run successfully. What do I miss please?

    1. seed says:

      Hey, I guess i got the answer.
      The helloworldClient project cannot see the dependency helloworld(if the helloworld module project is rightly completed) event if you add helloworld project to the buildpath at Modulepath(not classpath). Unless you add requires “packagename”.
      Just as described “As public is not just public, it supports very Strong Encapsulation”. HelloworldClient has no access to public class Helloworld if not declaring requires “com.hello”

  2. raman a says:

    This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge. Excellent post.

  3. Stephan Herrmann says:

    Any plans to update this tutorial? As the confusion about “Create module-info.java” demonstrates the information here does not reflect the functionality that was released as Oxygen.1a.

    1. Rolf Schumacher says:

      What about a link to a tutorial that meets current dialogs? Would have saved me some hours.
      Wonder why this tutorial has not been adapted or flagged as “out of date” at the top. Is it because real programmers don’t like modules?

  4. Dipannita Basu says:

    Hi,
    I am using eclipse Oxygen 4.7 and in the step of create source module, I am not getting checkbox to crate a module -info descriptor file.

    Please reply.

    1. Navneet says:

      Hi Dipannita,

      There is second way to create module-info file.

      Right click on your project and go to Configure > Create moule-info.java

  5. Navneet says:

    Hi,

    In Step of create source module, I am not getting checkbox to crate a module descriptor file, I am using eclipse oxygen. Which version you are using for this.

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