How to get module name by class in Java 9? For example, let's consider the following situation. There are two named modules - ModuleA and ModuleB. ModuleA knows nothing about ModuleB. ModuleB requires ModuleA.
ModuleA contains class:
public class ClassA {
public void printModuleName(Class klass) {
//how to get here name of the module that contains klass?
}
}
ModuleB contains class:
public class ClassB {
public void doIt() {
ClassA objectA = new ClassA();
objectA.printModuleName(ClassB.class);
}
}
How to do it?
To get a
Module
by aClass
in Java9, you can use thegetModule()
and further
getName
on theModule
class to fetch the name of the moduleAn observable point to note (not in your case as you're using named modules) is that the
getModule
returns the module that this class or interface is a member of.