java cmd测试_Java SE 9:使用CMD提示开发和测试简单模块(第3部分)

java cmd测试

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.

在我以前的文章中,我已经讨论了关于Java 9模块系统的一些理论概念:“ Java模块系统”,“模块”和“模块描述符”。 我将在本文中讨论“如何通过使用命令(CMD)提示符开发和测试简单的HelloWorld Java 9模块”。

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.

在这一系列“ Java 9 Module System”文章中,这是我的第三篇文章。 在阅读本文之前,请单击以下链接以浏览我的上一篇文章,以了解有关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

    “ HelloWorld”模块简介
  • Steps to Develop a Java 9 Module

    开发Java 9模块的步骤
  • Develop “HelloWorld” Module

    开发“ HelloWorld”模块
  • Develop “HelloWorldClient” Module

    开发“ HelloWorldClient”模块
  • Set Java SE 9 Environment

    设置Java SE 9环境
  • Compile “HelloWorld” Module

    编译“ HelloWorld”模块
  • Compile “HelloWorldClient” Module

    编译“ HelloWorldClient”模块
  • Test “HelloWorldClient” Module

    测试“ HelloWorldClient”模块

“ HelloWorld”模块简介 (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.

作为开发人员,我们首先从“ HelloWorld”程序开始学习新的概念或编程语言。 以同样的方式,我们开始通过“ HelloWorld”模块开发来学习Java 9新概念“ 模块化编程 ”。

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

在本文中,我们将开发以下两个模块:

  • com.hello

    com.hello
  • com.hello.client

    com.hello.client

Our requirements for these two modules are:

我们对这两个模块的要求是:

  • com.hello module develops “HelloWorld” component.

    com.hello模块开发“ HelloWorld”组件。
  • com.hello module exports com.hello package to outside world.

    com.hello模块将com.hello包导出到外部。
  • com.hello.client module requires or imports com.hello module.

    com.hello.client模块需要或导入com.hello模块。
  • com.hello.client module uses “HelloWorld” component.

    com.hello.client模块使用“ HelloWorld”组件。

This complete problem statement is depicted as shown below:

完整的问题陈述如下所示:

Let us start developing our modules in the coming sections.

让我们在接下来的部分开始开发模块。

开发Java 9模块的步骤 (Steps to Develop a Java 9 Module)

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

我们将一步一步地遵循这些步骤来开发和测试“ HelloWorld”模块。

  • Create Module name folder, for example “com.hello”.

    创建模块名称文件夹,例如“ com.hello”。
  • Create Module Packages, for example “com.hello”.

    创建模块包,例如“ com.hello”。
  • Create our Java component, for example “HelloWorld.java”.

    创建我们的Java组件,例如“ HelloWorld.java”。
  • Create Module Descriptor, for example “module-info.java”.

    创建模块描述符,例如“ module-info.java”。
  • Define Module Description in Module Descriptor, for example “exports com.hello;” of “module-info.java” in “com.hello” module.

    在模块描述符中定义模块描述,例如“ exports com.hello;” “ com.hello”模块中“ module-info.java”的内容。
  • 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.

这些步骤几乎适用于所有模块开发。 本文中使用的大多数命令都适用于Windows OS。 对于其他操作系统,如Linux,Mac,很少有命令会有所不同。

开发“ HelloWorld”模块 (Develop “HelloWorld” Module)

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

我们首先从“ HelloWorld”模块开发开始。 请参阅我们的问题陈述图以获取更多详细信息。

Create “HelloWorld” Module name folder: com.hello

创建“ HelloWorld”模块名称文件夹:com.hello

mkdir com.hello

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

创建“ HelloWorld”模块程序包名称文件夹: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”.

以包名称“ com.hello \ com \ hello”开发“ HelloWorld.java”组件。

HelloWorld.java

HelloWorld.java

package com.hello;

public class HelloWorld {

  public String sayHelloWorld() {

      return "Hello World!";
  }

}

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

在模块根文件夹“ com.hello”处开发模块描述符。

module-info.java

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”正在将“ com.hello”包导出到外界,以便我们的HelloWorldClient程序可以使用它。

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

“ com.hello”模块的完整树状结构如下图所示:

开发“ HelloWorldClient”模块 (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.

像“ HelloWorld”模块一样,我们需要遵循相同的步骤来开发此模块。 请参阅我们的问题陈述图以获取更多详细信息。

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

创建“ HelloWorldClient”模块名称文件夹:com.hello.client

mkdir com.hello.client

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

创建“ HelloWorldClient”模块程序包名称文件夹:com \ hello \ client

mkdir com.hello\com\hello\client

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

在包名称“ com.hello \ com \ hello \ client”下开发“ HelloWorldClient.java”组件。

HelloWorldClient.java

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”.

在模块根文件夹“ com.hello”处开发模块描述符。

module-info.java

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.

如果我们观察以上模块描述符,我们可以说“ com.hello.client”模块使用的是“ com.hello”包名称中可用的类型。 它没有向外界出口任何东西。 因此,其他模块无法访问在“ com.hello.client”程序包名称下定义的类型。

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

“ com.hello”和“ com.hello.client”都模块完整的树状结构,如下所示:

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

我们已经成功开发了两个模块。 是时候编译它们以进行测试了。

设置Java SE 9环境 (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 SE 9 EA(早期访问)软件之后,我们需要设置两个环境变量。

  • JAVA_HOME

    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.

当我将Java SE 8用于其他项目时,仅在命令提示符处设置了这两个变量。 请将这些变量设置为系统变量。

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工具(java,javac和jar)的更新以支持Java 9 Module System,请仔细阅读我的文章,以详细了解它们:

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

Java SE 9 Tools的更改(链接即将更新)

编译“ HelloWorld”模块 (Compile “HelloWorld” Module)

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

在本节中,我们将编译“ HelloWorld”第一个模块。

Please use the following javac command to compile this module:

请使用以下javac命令来编译此模块:

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:

现在,“ com.hello”模块的输出文件夹如下所示:

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.

由于我们的“ HelloWorldClient”模块使用此“ HelloWorld”模块,因此,我们应该有com.hello Module Jar文件在客户端模块中引用它。 让我们使用以下jar命令创建此文件。

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:

现在,我们的“ HelloWorld”模块被归档到一个jar文件中:com.hello.jar,它位于mlib文件夹中,如下所示:

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.

如果我们观察到如上所示的“ com.hello”模块jar文件的内容,我们可以看到我们的模块描述符已编译为“ module-info.class”文件。

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

编译“ HelloWorldClient”模块 (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.

在本节中,我们将编译“ HelloWorldClient”第二个模块。 Java SE 9的“ javac”命令支持“模块路径”选项来引用其他模块。

Please use the following javac command to compile this module:

请使用以下javac命令来编译此模块:

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.

我们知道我们的“ com.hello.jar”文件位于“ mlib”,“ com.hello.client”取决于“ com.hello”模块。

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.

为了在“ com.hello.client”编译过程中引用“ com.hello”模块,我们应使用“ module-path”来引用“ mlib”文件夹,如上所示。 没有这个路径,我们将无法编译“ com.hello.client”模块组件。

测试“ HelloWorldClient”模块 (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”模块组件了。

“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.

“ com.hello.client”模块具有Java组件:“ HelloWorldClient.java”,其中包含main()方法,因此我们可以使用“ java”命令照常运行该程序。

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.

哇,好消息!
现在,我们已经成功开发,编译和测试了“ HelloWorld”模块。

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.

关于“使用Command(CMD)提示开发和测试简单的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学习愉快!

翻译自: https://www.journaldev.com/13543/javase9-simple-module-cmd-prompt-part3

java cmd测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值