1. 程式人生 > >IntelliJ IDEA 中Java 9 模組化實戰

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