I'll leave some research regarding how javac
works with the option --patch-module
.
I. Valid --patch-module path and module name which is not in module path
$ javac --patch-module com.test.mdl.platform=mdl-plarform/src/main/java/ \
mdl-plarform/src/main/java/com/test/mdl/platform/Patch.java
error: module not found: com.test.mdl.platform
1 error
This fails.
Javac
applies regular module path scan to lookup the module specified in the left hand side of --patch-module
equality (com.test.mdl.platform
in this particular case).
For this module which is not in module path it obviously fails and the related module not found
error is reported. The module com.test.mdl.platform
is not in the module path so the behavior is expected.
II. Valid module name and fake path
$ javac --patch-module com.test.mdl.platform=some/fake/path/ \
mdl-plarform/src/main/java/com/test/mdl/platform/Patch.java
This works "ok".
The reason is that javac
checks the path specified in the right hand side of the --patch-module
argument for correctness. The path is correct iff it contains (either directly or indirectly) the file being compiled.
The check is performed in the com/sun/tools/javac/file/Locations.java. As can be seen it is simply loops over the Path
mdl-plarform/src/main/java/com/test/mdl/platform/Patch.java
getting parent on each iteration and comparing with the some/fake/path/
.
If the path is incorrect then null
is returned and the module is not being patched. The file is treated as belonging to the unnamed module in this case
III. Path exists, but contains neither module-info.java
nor module-info.class
$ javac --patch-module java.logging=mdl-plarform \
mdl-plarform/src/main/java/com/test/mdl/platform/Patch.java
This works ok.
The reason is that the module java.logging
is contained in the runtime image and can be found during module lookup. The next step is to find either module-info.java
or module-info.class
in the directory. In this case it fails since it does not contain it, then it falls back to lookup the module-info.class
in the runtime image which succeeds.
IV. Valid module name and module path, but module name mismatch
$ javac --patch-module java.logging=mdl-plarform/src/main/java \
mdl-plarform/src/main/java/com/test/mdl/platform/Patch.java
mdl-plarform/src/main/java/module-info.java:1: error: module name com.test.mdl.plarform does not match expected name java.logging
module com.test.mdl.plarform {
^
error: cannot access module-info
cannot resolve modules
2 errors
This fails.
After the module-info.java
was found in the directory specified in the --patch-module
it is then parsed and the module name it contains is checked for equality with the name specified in the --patch-module
. In this case we have a mismatch so the related error is printed.
I checked this behavior by simply debugging javac
with the regular java debugger. So the only intention of that was to explain what was going on in the cases described in the question.
com.test.mdl.platform
module with itself(or anothercom.test.mdl.platform
) possibly when you specify themodule-info.java
directory in the input. on the other side, for the latter, there is no such possibility given an empty or invalid directory. – Dowerjavac --patch-module com.test.mdl.platform=some/fake/path/
? and the other alternativejavac --patch-module com.test.mdl.platform=mdl-plarform/src/main/java/com/test/mdl/platform/Path.java
? – Dowerno input files
as expected. I tried a few more examples and noticed that examples likejavac --patch-module fake.module=fake/path mdl-plarform/src/main/java/com/test/mdl/platform/Patch.java
also work fine so it means invalid--patch-module
arguments seem to be simply ignored. – Uncivilizedjar
file. – Uncivilized--patch-module
you need two things: (1) and existing module, and (2) additional sources outside the module, that should be compiled as if belonging to the module. In the given layout I only see one module containing a file that is calledPatch.java
but not patching anything since it already belongs to the module. What am I missing? – Calamity