英文:
module Directory or module file inside name should be same or not
问题
以下是翻译好的内容:
目录结构
.
├── out
│ └── calc-module
│ ├── com
│ │ └── ngsharma
│ │ └── ocp
│ │ └── Calculator.class
│ └── module-info.class
└── src
└── calculator
├── com
│ └── ngsharma
│ └── ocp
│ └── Calculator.java
└── module-info.java
成功编译并生成out/calc-module目录。
javac -d out/calc-module src/calculator/module-info.java src/calculator/com/ngsharma/ocp/Calculator.java
module-info.java
module calc {}
Calculator.java
package com.ngsharma.ocp;
public class Calculator {
public static int add(int i1, int i2) { return i1 + i2; }
public static void main(String[] args) {
System.out.println(add(20,40));
}
}
当我使用以下命令编译这个模块时。
1. java -p out --add-modules calc-module com.ngsharma.ocp.Calculator
当我使用这个命令时,它会抛出FindException异常:找不到模块calc-module。
根据第1点,模块内部的名称和模块目录名称应该相同,否则会抛出FindException异常。
2. java -p out --add-modules calc com.ngsharma.ocp.Calculator
但在这种情况下,我没有使用模块目录名称,我只是在module-info.java文件中使用了模块名,并成功运行。为什么会这样?模块文件名和模块文件夹名应该相同,否则不能编译和运行。请有经验的人指导我,纠正我犯的错误。(对模块不熟悉)。
英文:
<u>Directory Structure</u>
<pre><font color="#729FCF"><b>.</b></font>
├── <font color="#729FCF"><b>out</b></font>
│ └── <font color="#729FCF"><b>calc-module</b></font>
│ ├── <font color="#729FCF"><b>com</b></font>
│ │ └── <font color="#729FCF"><b>ngsharma</b></font>
│ │ └── <font color="#729FCF"><b>ocp</b></font>
│ │ └── Calculator.class
│ └── module-info.class
└── <font color="#729FCF"><b>src</b></font>
└── <font color="#729FCF"><b>calculator</b></font>
├── <font color="#729FCF"><b>com</b></font>
│ └── <font color="#729FCF"><b>ngsharma</b></font>
│ └── <font color="#729FCF"><b>ocp</b></font>
│ └── Calculator.java
└── module-info.java
</pre>
Successfully compile and generate the out/calc-module directory.
<pre>javac -d out/calc-module src/calculator/module-info.java src/calculator/com/ngsharma/ocp/Calculator.java </pre>
module-info.java
<pre>module calc {}</pre>
Calculator.java
package com.ngsharma.ocp;
public class Calculator {
public static int add(int i1, int i2) { return i1 + i2; }
public static void main(String[] args) {
System.out.println(add(20,40));
}
}
When i compile this module in following command. <br>
1. java -p out --add-modules calc-module com.ngsharma.ocp.Calculator
When i'm using this command, it throw exception FindException: Module calc-module not found. <br>
According to 1. module inside name and module directory name should be same other throw FindException. <br>
2 java -p out --add-modules calc com.ngsharma.ocp.Calculator <br>
but in this situation i'm not using module directory name i'm using only module-info.java inside file name, and successfully run. How..? and Why..? <br> module file name and module folder name should be same, otherwise not compile and run it is corrent or not.Please anybody suggest me and correct me what is my mistake.(newbie in module).
答案1
得分: 1
这个(你的第一条命令)无法工作,因为没有名为 calc-module
的模块。模块的名称是 calc
,如在 module-info.java
中指定的那样。
以下两种方式都适用于您:
java -p out --add-modules calc com.ngsharma.ocp.Calculator
java -p out --module calc/com.ngsharma.ocp.Calculator
编译
直接父目录
如果所包含模块的目录名称与模块名称相同,则有一种简便的方法可以编译模块,正如这个结构中所示:
.
+-- src
+-- calc
+-- com
…
+-- Calculator.java
+-- module-info.java
现在可以使用以下命令编译整个模块:
javac -d out --module-source-path src -m calc
out 目录结构将自动具有模块的名称,因此与 src 结构相同。
如果目录名称与模块名称不相同,则此命令将不起作用,在这种情况下,您将得到 error: module calc not found in module source path
错误。
程序的运行方式没有任何区别。这仅适用于编译阶段。
非直接父目录
使用 --module-source-path
不要求包含模块名称的目录名称是直接父目录,您还可以创建这种常见结构,其中模块名称位于根目录的若干级别上:
.
+-- calc
+-- src
+-- main
+-- java
+-- com
…
+-- Calculator.java
+-- module-info.java
但在这种情况下,您需要调整编译命令(正如 Stephan Herrmann 在对“Java 9 error: not in a module on the module source path”的回答中解释的那样):
javac -d out --module-source-path "./*/src/main/java/" -m calc
请注意,并没有要求目录与模块本身的名称相同。这只是有助于组织您的程序并使编译更容易。
英文:
This (first of your commands) doesn't work because there is no module named calc-module
. The name of the module is calc
as specified in the module-info.java
java -p out --add-modules calc-module com.ngsharma.ocp.Calculator <-- cannot work
Either of these two will work for you:
java -p out --add-modules calc com.ngsharma.ocp.Calculator
java -p out --module calc/com.ngsharma.ocp.Calculator
Compiling
Immediate parent directory
There is a way to compile modules easily if the directory name of the contained module is identical to the module name. As in this structure:
.
+-- src
+-- calc
+-- com
¦ +-- ngsharma
¦ +-- ocp
¦ +-- Calculator.java
+-- module-info.java
Now the entire module can be compiled using:
javac -d out --module-source-path src -m calc
The out directory structure will automatically have the name of the module, and hence be identical to the src structure.
This command will not work if the directory name is not identical to the module name, and in such case you will get error: module calc not found in module source path
.
There is no difference in how the program is run. This is only valid for compilation.
Not immediate parent directory
The use of --module-source-path
doesn't require that the directory name identical to the module name is the immediate parent directory to your package though. You can also create this common structure, where the module name is at the root several directories up:
.
+-- calc
+-- src
+-- main
+-- java
+-- com
¦ +-- ngsharma
¦ +-- ocp
¦ +-- Calculator.java
+-- module-info.java
However you need to tweek the compile command is this case (as Stephan Herrmann explains in an answer to "Java 9 error: not in a module on the module source path"):
javac -d out --module-source-path "./*/src/main/java/" -m calc
Note that there is no requirement that the directory shares the name with the module itself. It just helps to structure your program and make compiling easier.
答案2
得分: 0
后面的命令
java -p out --add-modules calc com.ngsharma.ocp.Calculator
可以正常运行,因为 --add-modules
参数接受的是模块路径(out
目录)上存在的模块的名称,本例中为 calc
,而不是包含 module-info.java
的文件夹的名称。
直接回答问题,没有强制要求将构建模块的目录命名为模块名称。
英文:
The latter command
java -p out --add-modules calc com.ngsharma.ocp.Calculator
works since the --add-modules
accepts as argument the name of modules present on the modulepath (out
directory) which in this case is calc
and not the name of the folder within which the module-info.java
resides.
To answer the question directly, there is no mandate to name the directory where you build the module same as the module name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论