为什么 Java 无法运行类 - 启动层初始化期间发生错误 - 未找到模块 mods

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46747976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:20:45  来源:igfitidea点击:

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

javajava-9

提问by rashmi mardur

I have gone through the java 9 jigsaw tutorial. I have been struggling to run class, java throws below error-

我已经阅读了java 9 jigsaw 教程。我一直在努力运行课程,java 抛出以下错误-

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 command-

Javac 命令-

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

I am using mac, java version-

我使用的是 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)

Am I missing anything?

我错过了什么吗?

回答by Rahul Sharma

Remove additional mods from module name-

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

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

回答by M Anouti

The -mflag accepts the module name and the main class you want to run. The module name is com.test, hence the command to run the class should be:

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

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

The --module-path modstells javawhere to search for to find com.test.

--module-path mods通知java在哪里搜索找到com.test

回答by Naman

When you first compiled your code using javaccommand as-

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

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

What you ensured using -d directorywas that to

你确保使用的-d directory

Set the destination directory for class files.

设置类文件的目标目录。

in your case the modsfolder where

在你的情况下,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.

如果类是包的一部分,则 javac 将类文件放在反映包名称的子目录中,并根据需要创建目录。

Hence you can take a look at the mods directory after executing the command, the .classfor all(*.java) would exist in the corresponding directory structure as of their package name.

因此,您可以在执行命令后查看 mods 目录,.classfor all( *.java) 将存在于其包名称的相应目录结构中。



Then javatool option --module-path module pathor -p module pathspecified in your next command :

然后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.

从以分号 (;) 分隔的目录列表中搜索目录。每个目录都是一个模块目录。

Your listed directory according to which is mods, assuming which you must have created following the getting started link.

您列出的目录根据 which is mods,假设您必须按照入门链接创建哪个。



Followed by --module modulename[/mainclass]or -m module[/mainclass]in your command

跟随--module modulename[/mainclass]-m module[/mainclass]在您的命令中

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

如果模块未指定,则指定要解析的初始模块和要执行的主类的名称。

which in your case is the module name com.testand the main class com.test.HelloWorld.

在您的情况下是模块名称com.test和主类com.test.HelloWorld



Hence the complete correct syntax of the command shall be:-

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

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

回答by user3345469

Thanks for the info

谢谢(你的)信息

2 things I was doing incorrectly

我做错的两件事

1) Using the package path again like below

1)再次使用包路径,如下所示

module\package\package.class

模块\包\包.class

We should not retype the package path while executing the class.

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

2) Using the dos styled backslash instead of forward slash. We should always use the forward slash (/) to separate module with the class even you are executing in windows environment.

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

module/package.class

模块/包.class