Java 9 模块系统
创建模块
接下来我们创建一个 com.bootwiki.greetings 的模块。
第一步
创建文件夹 C:\>JAVA\src,然后在该目录下再创建与模块名相同的文件夹 com.bootwiki.greetings。
第二步
在 C:\>JAVA\src\com.bootwiki.greetings 目录下创建 module-info.java 文件,代码如下:
module com.bootwiki.greetings { }
module-info.java 用于创建模块。这一步我们创建了 com.bootwiki.greetings 模块。
第三步
在模块中添加源代码文件,在目录 C:\>JAVA\src\com.bootwiki.greetings\com\runoob\greetings 中创建文件 Java9Tester.java,代码如下:
package com.bootwiki.greetings; public class Java9Tester { public static void main(String[] args) { System.out.println("Hello World!"); } }
第四步
创建文件夹 C:\>JAVA\mods,然后在该目录下创建 com.bootwiki.greetings 文件夹,编译模块到这个目录下:
C:/>JAVA> javac -d mods/com.bootwiki.greetings src/com.bootwiki.greetings/module-info.java src/com.bootwiki.greetings/com/runoob/greetings/Java9Tester.java
第五步
执行模块,查看输出结果:
C:/>JAVA> java --module-path mods -m com.bootwiki.greetings/com.bootwiki.greetings.Java9Tester Hello World!
module-path 指定了模块所在的路径。
-m 指定主要模块。