为什么 Java 无法 运行 class- 引导层初始化期间发生错误- 未找到模块模组

Why Java fail to run class- error occurred during initialization of boot layer- Module mods not found

我经历了java 9 jigsaw tutorial。我一直在努力 运行 class, java throws below error-

java --module-path mods -m mods/com.test/com.test.HelloWorld
Error occurred during initialization of boot layer
java.lang.module.FindException: Module mods not found

Javac 命令-

javac -d mods --module-source-path src $(find src -name '*.java')

我正在使用 mac, java 版本-

$ java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

我错过了什么吗?

-m 标志接受模块名称和您想要 运行 的主要 class。模块名称是 com.test,因此 运行 class 的命令应该是:

java --module-path mods -m com.test/com.test.HelloWorld

--module-path mods 告诉 java 到哪里搜索才能找到 com.test

从模块名称中删除其他模组-

java --module-path mods -m com.test/com.test.HelloWorld

当您第一次使用 javac 命令编译代码时-

javac -d mods --module-source-path src $(find src -name '*.java')

您使用 -d directory 确保的是

Set the destination directory for class files.

在你的例子中 mods 文件夹

If a class is part of a package, then javac puts the class file in a subdirectory that reflects the package name and creates directories as needed.

所以执行命令后可以看一下mods目录,.class for all(*.java)根据包名存在对应的目录结构


然后 java 工具选项 --module-path module path-p module path 在您的下一个命令中指定:

Searches for directories from a semicolon-separated (;) list of directories. Each directory is a directory of modules.

您列出的目录是 mods,假设您必须在入门后创建 link。


在你的命令中--module modulename[/mainclass]-m module[/mainclass]后跟[=​​31=]

Specifies the initial module to resolve and the name of the main class to execute if not specified by the module.

在您的例子中是模块名称 com.test 和主要 class com.test.HelloWorld.


因此该命令的完整正确语法应为:-

java --module-path mods    -m     com.test/com.test.HelloWorld
                   ^               ^         ^
           module directory  module name   main class

感谢提供信息

2 件事我做错了

1) 像下面一样再次使用包路径

module\package\package.class

我们不应该在执行 class 时重新输入包路径。

2) 使用 dos 样式的反斜杠而不是正斜杠。即使您在 windows 环境中执行,我们也应该始终使用正斜杠 (/) 将模块与 class 分开。

module/package.class