Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export all packages from Java 9 module? [duplicate]

Right now, for every module I have, I need to explicitly specify packages I want to export. For example:

module core {
    exports cc.blynk.server.core;
    exports cc.blynk.server.core.protocol.handlers.decoders;
    exports cc.blynk.server.core.protocol.handlers.encoders;
}

However, it is not very convenient. I would like to do something like that:

module core {
    exports cc.blynk.server.core.*;
}

Is there any way to do that? Where this limitation comes from?

like image 675
Dmitriy Dumanskiy Avatar asked Sep 30 '17 12:09

Dmitriy Dumanskiy


People also ask

Can we export same packages from different modules?

The same Java package can only be exported by a single Java module at runtime. In other words, you cannot have two (or more) modules that export the same package in use at the same time.

How do I export a module in java?

There are two types of "export" clause can be used in "module-info. java" file. 1) export <package>: By default, the type public of a module is no longer visible outside the module. To make the public types of a given package visible from other modules, we must export this package.

How do I use java 9 modules?

To set up a module, we need to put a special file at the root of our packages named module-info. java. This file is known as the module descriptor and contains all of the data needed to build and use our new module. We start the module declaration with the module keyword, and we follow that with the name of the module.

What is a module-info java file?

module-info. java file. It declares the dependencies within the module system and allows the compiler and the runtime to police the boundaries/access violations between the modules in your application.


Video Answer


1 Answers

No, you can not use a wildcard to export all packages within the module. You will have to export each package explicitly.

It is not allowed since this could majorly lead to conflicts in the different packages exported from different modules which defies the purpose of modularising the code.


Additionally quoting from one of the threads:

The packages exported by a module are meant to be a stable API that consumers can rely on. For this reason, we make the module author spell out the exported packages explicitly. This also dials down the likelihood of multiple modules needlessly exporting the same package. Additionally, it avoids the confusion that would occur if com.abs.* was exported without qualification while com.abs.foo was exported with qualification.

like image 134
Naman Avatar answered Oct 13 '22 07:10

Naman