本文共 8470 字,大约阅读时间需要 28 分钟。
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.
我之前的文章中已经讨论过“ Java模块系统”基础知识。 我将在本文中讨论“如何使用两种流行的IDE开发和测试简单的HelloWorld Java 9模块:Eclipse和IntelliJ IDEA”。
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.
在“ Java模块系统”系列文章中,这是我的第四篇文章。 在阅读本文之前,请单击以下链接以浏览我的上一篇文章,以了解有关Java 9 Modules的一些基础知识。
Now, Let us start developing HelloWorld and it’s clients Modules.
现在,让我们开始开发HelloWorld及其客户端模块。
I have already discussed about “HelloWorld” and “HelloWorldClient” Modules requirements in my third post’s first section. Please read once before going through the next sections.
在第三篇文章的第一部分中,我已经讨论了“ HelloWorld”和“ HelloWorldClient”模块的要求。 在进行下一部分之前,请先阅读 。
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.
我将使用“ Eclipse Oxygen M4”和“ JDK 1.9 EA(Early Access)”来开发我们的应用程序。 请下载并设置Eclipse和JDK软件。
Add JDK 9 Path to “Eclipse Installed JREs” as shown below:
将JDK 9路径添加到“ Eclipse Installed JRE”,如下所示:
This step creates default module-info.java file at Module root folder as shown below:
此步骤在模块根文件夹下创建默认的module-info.java文件,如下所示:
package com.hello;public class HelloWorld { public String sayHelloWorld() { return "Hello World!"; }}
module com.hello { exports com.hello; }
That’s it about “HelloWorld” Module development. Final “HelloWorld” Module project structure looks like below:
关于“ HelloWorld”模块开发就是这样。 最终的“ HelloWorld”模块项目结构如下:
Let us move to “HelloWorldClient” Module development in next section.
在下一节中,让我们转到“ HelloWorldClient”模块开发。
Fallow Same “HelloWorld” Module steps to “HelloWorldClient” Module.
相同的“ HelloWorld”模块进入“ HelloWorldClient”模块。
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()); }}
module com.hello.client { requires com.hello; }
As “com.hello” Module is not in “com.hello.client” build path, it is showing that error message.
由于“ com.hello”模块不在“ com.hello.client”构建路径中,因此显示该错误消息。
Let us add “com.hello” project to “com.hello.client” build path as shown below:
让我们将“ com.hello”项目添加到“ com.hello.client”构建路径,如下所示:
It resolves Module dependency and also solves import statements issues.
它解决了模块依赖性,还解决了导入语句问题。
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:使用Eclipse IDE测试“ HelloWorld”模块
要测试我们的模块,请右键单击“ HelloWorldClient.java”,然后选择“运行方式” >>“ Java应用程序”以查看以下输出:Hello World!
It is similar to our “CMP Prompt” example. There is no difference between Part-3 and this example, except Commands and IDEs.
它类似于我们的“ CMP Prompt”示例。 第3部分与本示例之间没有什么区别,除了命令和IDE。
To develop and test “HelloWorld” Module, I’m going to use latest version of IntelliJ IDEA: IDEA 2017.1 EAP.
为了开发和测试“ HelloWorld”模块,我将使用IntelliJ IDEA的最新版本: IDEA 2017.1 EAP 。
You can download this software from this link:
https://confluence.jetbrains.com/display/IDEADEV/IDEA+2017.1+EAP您可以从以下链接下载此软件:
https://confluence.jetbrains.com/display/IDEADEV/IDEA+2017.1+EAPClick on “Next” and “Next” buttons.
单击“下一步”和“下一步”按钮。
Click on “Finish” button. This step creates our base project.
点击“完成”按钮。 此步骤将创建我们的基础项目。
Then create a package “com.hello” and Create “HelloWorld.java” file.
然后创建一个包“ com.hello”并创建“ HelloWorld.java”文件。
NOTE:- Please copy content from Eclipse IDE examples to “module-info.java” and “HelloWorld.java” files.
注意:-请把内容从Eclipse IDE示例复制到“ module-info.java”和“ HelloWorld.java”文件中。
Create and copy content from from Eclipse IDE examples to “module-info.java” and “HelloWorldClient.java” files.
创建内容并将内容从Eclipse IDE示例复制到“ module-info.java”和“ HelloWorldClient.java”文件中。
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).
现在,我们已经成功开发了两个模块。 但是,如果您在IDE的左下角观察到,我们将看到一个错误(类似于Eclipse IDE错误)。
To resolve this issue, we need to add “com.hello” Module as dependency.
要解决此问题,我们需要添加“ com.hello”模块作为依赖项。
Click on “Open Module Settings” option as shown below:
单击“打开模块设置”选项,如下所示:
Add “com.hello” Module.
添加“ com.hello”模块。
Click on “OK” button. It resolves all issues.
点击“确定”按钮。 它解决了所有问题。
You can see the output and complete Project and Modules structure in the below diagram
您可以在下图中看到输出以及完整的Project和Modules结构
Wow, Congratulations!
哇,恭喜!
We are successfully developed and tested our Favourite “HelloWorld” Module example using two popular IDEs.
我们已经使用两个流行的IDE成功开发并测试了我们最喜欢的“ HelloWorld”模块示例。
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.
这就是“使用Eclipse和IntelliJ IDEA IDE开发和测试简单的HelloWorld Java 9模块”的全部内容。 在我的后续文章中,我们将讨论有关Java SE 9 Modules Development的更多新概念和复杂概念。
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!
Java SE 9学习愉快!
翻译自:
转载地址:http://eyqzd.baihongyu.com/