Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method getModule returning unnamed module for class in named module

Tags:

I just started to have a look at the Java 9 module system and I was wondering whether it is possible for a class to know in which module it is located.

Therefor I created the following module

module de.test {
  exports de.test.myexport;
}

and compiled a jar file that looks like

> jar --print-module-descriptor --file=Java9Test-1.0-SNAPSHOT.jar                                

de.test
  requires mandated java.base
  exports de.test.myexport

In package de.test, I have a class called Overview where I'm calling

Module module = Overview.class.getModule();

However, the returned module object is unnamed and has no ModuleDescriptor.

Am I using getModule() correctly here, or is there any other way to load the module of a class?

I'm using JDK 9 build 120 on OS X.

like image 260
Jan Gassen Avatar asked May 31 '16 14:05

Jan Gassen


1 Answers

All JARs on the class path (with java --class-path ...) get bundled into the same so-called unnamed module, regardless of whether they are "a real module" or "just a JAR". When you ask a class from such a JAR for its module, you get the result you describe.

Try putting the JAR on the module path (with java --module-path ...) and Class::getModule should return what you expect.

like image 79
Nicolai Parlog Avatar answered Nov 09 '22 12:11

Nicolai Parlog