TL;DR — A module-info.java file is not designed to be a stand-alone thing. It is designed to be part of a multi-source file compilation unit. A unit which includes the module descriptor, plus at least one other source file.
The long-winded version
„…there's another folder called myPackage
in which my Main.class
exists…“
I'm gonna assume you meant to type Main.java
and that Main.class
is an oversight since…
- You don't mention anything about it being an already-compiled byte code file.
- I can't think of a reason why anybody would intentionally put a .class file in a source directory.
I'm also gonna assume — although you didn't mention it at all in your question — that you're intentionally following the convention recommended in the Project Jigsaw: Module System Quick-Start Guide…
„…By convention, the source code for the module is in a directory that is the name of the module…“
I've established that compilation would succeed if your myModule
module is just an empty module{}
block. That is, if it doesn't declare that it exports myPackage
. So I'm gonna also assume your module-info.java, like my experimental one, contains…
module myModule {
exports myPackage;
}
Keep in mind what the javac
tool is designed to do…
Description
The javac
command reads source files that contain module, package and type declarations written in the Java programming language…
Also consider the reliable configuration goal of the JPMS…
Reliable configuration, to replace the brittle, error-prone class-path mechanism with a means for program components to declare explicit dependences upon one another
If I sign a contract to mow your lawn every Saturday but then never turn up, how reliable am I? Imagine if it were legal for javac
to compile a module-info.java file for a module that claimed to export a package, but that package had no classes in it. How reliable is such a module?
If my build downloaded your hypothetical myModule
artifact from Maven Central, I could have an import myPackage.*
in my class only to find there's nothing in it. What use is a package with nothing in it?
„The problem is when I want to compile my module-info.java with the following command:“
javac module-info.java
The clue to the mystery is the fact that the above command results in the same error when the myPackage
directory contains legal source code.
The error message isn't telling you that the file system directory is empty. It's telling you that the package that you claim the module exports is empty. Empty as in: The compiler doesn't know anything about source code you never pass to it as part of a compilation unit.
„…the error disappears. I can't understand why this should happen?…“
By executing: javac module-info.java myPackage/Main.java
, you're composing a compilation unit made up of the things the module declares it exports. Which is the way the JPMS documentation specifies the system is expected to be used…
$ javac -d mods/com.greetings \
src/com.greetings/module-info.java \
src/com.greetings/com/greetings/Main.java
So typos notwithstanding, the cause of the error you report is pretty simple: Compiling a module that doesn't contain any source files is not only pointless, as far as the compiler is concerned it is a malformed module. Therefore, it's illegal.