I want to use java 9 modules in my project. I want to put my data and service classes (in package named com.test.base
) in one module named (Base), and put other things like main class and GUI (in package named com.test.main
) in another module named (Main).
- How to declare simple module (Base)?
- How to declare module (Main) with dependency of (Base) module?
I also want to use (Base) module with reflection.
Assume my root package is
com.test
so what directory structure should I use for these modules?
The jigsaw quick-start is a nice place to start off with this to help you out. To answer the specific questions:
Create a class named
module-info.java
withincom.test.base
directory with the definition as:This exports the
com.test.base
package.Same as the other module
base
a class namedmodule-info.java
withincom.test.main
directory with the definition as:Makes use of the exported package.
Depending on yoru usage, if the API os removed or changes as listed here, then you can use the
--add-exports
java option as detailed in the JEP-261I would also suggest reading Nicolai's answer to How to solve InaccessibleObjectException ("Unable to make {member} accessible: module {A} does not 'opens {package}' to {B}") on Java 9? for a nice explanation over how to make use of reflection in Java-9.
Your directory structure should be somewhat like: