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
1 2 3 4 5 6 7 8 |
package com.hello; public class HelloWorld { public String sayHelloWorld() { return "Hello World!"; } } |
- Export package to other Modules in module-info.java as shown below:
1 2 3 4 5 |
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.
1 2 3 4 5 6 7 8 9 10 |
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.
1 2 3 4 5 |
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:
1 2 3 |
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!