有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    {a1}来自文件:

    Returns a module finder that locates modules on the file system by searching a sequence of directories and/or packaged modules. Each element in the given array is one of:

    1. A path to a directory of modules.
    2. A path to the top-level directory of an exploded module.
    3. A path to a packaged module.

    The module finder locates modules by searching each directory, exploded module, or packaged module in array index order. It finds the first occurrence of a module with a given name and ignores other modules of that name that appear later in the sequence.

    If an element is a path to a directory of modules then each entry in the directory is a packaged module or the top-level directory of an exploded module [emphasis added]. It is an error if a directory contains more than one module with the same name. If an element is a path to a directory, and that directory contains a file named module-info.class, then the directory is treated as an exploded module rather than a directory of modules [emphasis added].

    The module finder returned by this method supports modules packaged as JAR files. A JAR file with a module-info.class in its top-level directory, or in a versioned entry in a multi-release JAR file, is a modular JAR file and thus defines an explicit module. [...]

    [...]

    • 使用 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文件

      • 使用Maven等构建工具时也是如此。target/classes目录是一个分解模块,因为module-info.class文件存在于顶级目录中

    不幸的是,我找不到任何说明上述ModuleFinder实现保证在执行java时在内部使用。然而,在所有可能的情况下,以上就是所使用的