How to import classes from another module?

Viewed 50

I'm having a multi module Java project. To test specific constraints, I'm using Arch Unit. This works fine for the classes that are in the same module as the Arch Unit test class. However, I'd like to write one Arch Unit test that tests classes from all the modules in my project.

How can I import all classes from all the modules I have?

1 Answers

A module is a package of java packages. Let's say you have modules 'A' and 'B' and inside them you have packages 'a' and 'b' respectively, and inside of packages you have classes 'A.java' and 'B.java' respectively. If you need to use class 'A.java' from module 'A' inside of module 'B' first you need to edit 'module-info.java' file of module 'B'. It's location is at the root of the module and it will look like this:

module B {
requires A;

}

Now, module 'B' has both a runtime and a compile-time dependency on module 'A' and all public types exported from a dependency are accessible by our module when we use this directive.

In the 'module-info.java' file of module 'A' you need to write:

module A {
exports a;

}

For more detailed info refer to java modules guide

Related