Java: Cannot read package from the unnamed module?

While moving a project to Gradle, I stopped using my custom build of org.json which had a module-info.java fitted to it to comply to the module system. Now, I am using it via Maven normally, and as org.json is not a module by default, it gets put into the unnamed module.

My module-info looks like this:

open module mymodule {
    requires java.desktop;
    requires java.logging;
}

I am getting the error:

SomeSourceFile.java: error: package org.json is not visible
import org.json.*;
          ^
  (package org.json is declared in the unnamed module, but module mymodule does not read it)

This is logical, except I don't know why my module doesn't read the unnamed module (the purpose of the unnamed module is full backwards compatibility with non-modular software so all packages are exported etc.), and how I could make my module read the unnamed module. As you can see, I have already tried making my module open to no avail.

Upvotes: 4

Views: 6968

Answers (1)

Thiago Henrique Hupner
Thiago Henrique Hupner

Reputation: 482

Probably updating the Maven Compiler Plugin (to 3.8.1, for instance) will do the trick.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>
   </plugins>
</build>

The other thing will be to require the JSON module. From their pom.xml, I can see that they declare the Automatic-Module-Name as org.json (https://github.com/stleary/JSON-java/blob/master/pom.xml#L186)

So your module-info.java will become like this:

open module mymodule {
    requires java.desktop;
    requires java.logging;
    requires org.json;
}

Upvotes: 1

Related Questions