IntelliJ IDEA 中Java 9 模块化实战

右键-New-Module,输入模块基本信息

在这里插入图片描述

新建module-info.java文件

在这里插入图片描述

输入module-info.java内容如下:

module helloworld {
    exports com.hello;
}

新建HelloWorld.java文件

package com.hello;

import java.util.Calendar;

public class HelloWorld {

    public String sayHelloWorld() {

        return String.format("%s, now is %s", "hello world", Calendar.getInstance().getTime());
    }

}

最终模块内容如下

在这里插入图片描述

参照上述步骤新建模块helloworldclient

其中,module-info.java内容如下:

module helloworldclient {
    requires com.hello;
}

HelloWorldClient.java内容如下:

package com.hello.client;

import com.hello.HelloWorld;

public class HelloWorldClient {
    public static void main(String[] args) {
        HelloWorld hello = new HelloWorld();
        System.out.println(hello.sayHelloWorld());
    }
}

执行HelloWorldClient,得到如下结果

hello world, now is Thu Nov 15 11:52:29 CST 2018
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章