moduleinfo在哪个目录中运行。爪哇围棋?
从projectJigsaw的快速入门教程中,似乎可以看到模块信息。java进入一个带有模块名的目录,例如,对于my模块。包含类MyClass的模块:
bin/
my.module/
module-info.class
my/
module/
MyClass.class
然后,仍然遵循教程,在调用MyClass时(假设它有一个method
),模块路径选项被赋予bin
目录:
java -p path/to/bin -m my.module/my.module.MyClass
到目前为止,一切顺利
但是,如果我写下:
java -p path/to/bin/my.module -m my.module/my.module.MyClass
我不明白为什么两者都能起作用
如果bin
包含两个模块,则没有区别:如果两个目录都在模块路径中,则第二个版本仍然有效(如果只有bin
在模块路径中,它也有效,如教程中所述)
现在,当我尝试使用Eclipse创建Maven项目时,使用Java9模块,模块信息。java驻留在src/man/java
目录中,同样地,模块信息也驻留在该目录中。类位于target/classes
中。现在是包含模块信息的目录。类文件与模块的名称没有关系
并且target/classes
也在模块路径中(当我检查构建配置时)。这与上面的第二种情况类似,是java -p path/to/bin/my.module -m my.module/my.module.MyClass
。
显然,Java只使用模块信息的内容。类来发现模块名,而目录名是任意的
但是,在第一种情况下,当我写java -p path/to/bin -m my.module/my.module.MyClass
时,这意味着Java将在模块路径的目录中查找目录
我觉得我错过了一些重要的事情。对于Java在模块路径的目录中查找模块的方式,是否有明确的解释?什么真正指定了一个模块?(我的印象是:不是目录,只有module info.java)
使我更加困惑的是:Oracle documentation说:
--module-path modulepath... or -p modulepath
A semicolon (;) separated list of directories in which each directory is a directory of modules.
但显然这并不是全部,至少Eclipse使用这个选项的方式不是这样
注意:我已经看到了this question,但它并没有真正的帮助:一个答案是模块信息。java进入src/java/,另一个表示它进入一个名为模块名的目录。显然,这两种观点都是正确的,但它们没有解释原因,而且存在明显的矛盾
# 1 楼答案
{a1}来自文件:
使用
module-path path/to/bin
时,路径条目被视为模块的目录。这是因为bin
目录(即顶级目录)中没有module-info.class
文件my.module
目录重命名为您想要的任何内容;ModuleFinder
只是查看目录以查看是否存在module-info.class
文件李>使用
module-path path/to/bin/my.module
时,路径条目被视为分解模块。这是因为在my.module
目录(即顶级目录)中存在一个module-info.class
文件target/classes
目录是一个分解模块,因为module-info.class
文件存在于顶级目录中李>不幸的是,我找不到任何说明上述
ModuleFinder
实现保证在执行java
时在内部使用。然而,在所有可能的情况下,以上就是所使用的