2

Directory Structure

.
├── out
│   └── calc-module
│       ├── com
│       │   └── ngsharma
│       │       └── ocp
│       │           └── Calculator.class
│       └── module-info.class
└── src
    └── calculator
        ├── com
        │   └── ngsharma
        │       └── ocp
        │           └── Calculator.java
        └── module-info.java

Successfully compile and generate the out/calc-module directory.

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));
    }
}

When i compile this module in following command.
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.
According to 1. module inside name and module directory name should be same other throw FindException.
2 java -p out --add-modules calc com.ngsharma.ocp.Calculator
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..?
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).

Omid
  • 5,823
  • 4
  • 41
  • 50
Ng Sharma
  • 2,072
  • 8
  • 27
  • 49
  • *"According to java Doc module file name and module folder name should be same"*, do share a reference for your quote. – Naman Mar 16 '20 at 08:10
  • "According to" what? This sentence is not making sense.. Apart from that "module file name" is always `module-info.java` (and `module-info.class`). – Scratte Mar 16 '20 at 08:38

2 Answers2

1

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.

Scratte
  • 3,056
  • 6
  • 19
  • 26
0

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.

Naman
  • 27,789
  • 26
  • 218
  • 353