2

I could not find any mention of this in the JLS, it only talks about how compilation units are placed in the unnamed module when they are in the unnamed package.

Background: Many libraries require user code to put a configuration file with a specific name in the unnamed (default) package. I want to know if the JLS guarantees that these resources can be found via ClassLoader::getSystemResourceAsStream when using modules. It seems to work, but I'd like to have a source.

It would make sense, because there is no syntax for opens "", so the only way to be explicit would be to open the module itself.

1 Answers1

1

I think it's pretty clear.

An unnamed module exports every package whose ordinary compilation units are associated with that unnamed module.

An unnamed module opens every package whose ordinary compilation units are associated with that unnamed module.

Regarding the association:

The host system must associate ordinary compilation units in an unnamed package with an unnamed module (§7.7.5), not a named module.

Source

Note: it is talking about exporting the package, not exporting the compilation units, so resources in the package are covered by this statement.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • I should have been more clear, I'm talking about a named module that contains resources in the unnamed package. Edit: I edited the question to make it clearer. – Thomas Zimmermann Sep 27 '19 at 10:14
  • Quote 2 covers that. If they are in the unnamed package in a named module, then they are still placed in the unnamed module – Michael Sep 27 '19 at 10:17
  • Resources are not compilation units, or am I mistaken? – Thomas Zimmermann Sep 27 '19 at 10:22
  • 1
    I think what you are missing is that the compilation units of the unnamed package are associated with the unnamed module *regardless of whether there are any*. This association in turn means that the entire package is exported and opened. – Michael Sep 27 '19 at 10:28
  • 1
    @ThomasZimmermann Keep in mind that resources are also subject to _encapsulation_, as described [here](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Module.html#getResourceAsStream(java.lang.String)). A resource that's not in a package is not encapsulated. – Slaw Sep 27 '19 at 12:10
  • @Slaw Thanks for linking the rules. I thought they referred to invalid package names like META-INF (dashes are not allowed in package names), the unnamed package is a valid package, no? – Thomas Zimmermann Sep 27 '19 at 12:29
  • 1
    @Thomas The rules include: "_If the resource is not in a package in the module then the resource is not encapsulated_". This indicates that resources may or may not belong to a package. Combine that with "_An ordinary compilation unit that has no package declaration, but has at least one other kind of declaration, is part of an unnamed package_" from [§7.4.2 Unnamed Packages](https://docs.oracle.com/javase/specs/jls/se13/html/jls-7.html#jls-7.4.2), which only mentions ordinary compilation units can be part of an unnamed package. As Michael pointed out, resources are not such units. – Slaw Sep 27 '19 at 12:43