Modules   «Prev  Next»

Reusing Module in Java 9

Question 1: Where is that package imported from?
Now you might say the answer is obviously java.base because I have previously discussed this, that java.base exports the java.lang package and you are right, in general.

Question2: How does the compiler know that?
Should it search the file system looking for modules which export the imported package? What if multiple modules export the imported package. If we intends to reuse the java.base module, we should document that fact. Actually, since we are writing a module ourselves, we must document that fact. This is the second feature of modules in JDK 9, reliable dependencies.

Reusing a Module
Reusing Module: A module is a set of packages that reuses the packages exported by other modules


Module = set of packages that reuses packages exported by other modules

A module is not just a set of packages, it is a set of packages that reuses the packages exported by other modules.
So the HelloWorld module specifies that it reuses the java.base module with a requires directive, in its
module-info.java.

This means that code in the HelloWorld module can import any of the packages exported by java.base.
Question: What happens if code in the HelloWorld module tries to import any of the packages concealed by java.base such as
sun.security.provider
in red?

module hello.world{
exports com.example.hello;
requires java.base;
}