IntelliJ IDEA 中Java 9 模組化實戰
阿新 • • 發佈:2019-01-04
右鍵-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