Skip to content
Advertisement

org.json cannot be resolved to a module?

I’m learning Java. To read JSON in my application, I downloaded this JSON library; which is an automatic module.

I included that library in my module descriptor like:

module art
{
    exports  art.anixt;
    exports  art.coartl;
    exports  art.runeape;
    requires org.json;    // org.json cannot be resolved to a moduleJava(8389908)
}

My settings.json in vscode:

{
    "files.exclude": {
        "**/.classpath": true,
        "**/.project": true,
        "**/.settings": true,
        "**/.factorypath": true
    },
    "java.format.settings.url": "Format.xml",
    "java.format.settings.profile": "style",
    "java.project.referencedLibraries": [
        "lib/**/*.jar" // jar file showing in Referenced library(see screenshot)
    ]
}

How do I include the jar file in my module and import it into my Java file?

Screenshot:

screenshot

Advertisement

Answer

TL;DR — As this unresolved ‘Cannot be resolved’ errors in projects with module-info.java issue reports, vscode is brain dead when it comes to JPMS and module-info.java.


The long-winded version

From my own experience, I can personally vouch for what the reporter of the above-linked vscode issue reports…

…I’ve tried both Gradle and Maven…

…I find that Gradle and Maven will automatically refresh the classpath file and remove my modifications to it, which will bring back the errors…

…there needs to be module path information set in the classpath file in order for Eclipse to be happy, but there is no good way to do with that from Gradle or Maven…

Proof that it’s a vscode issue is that the exact same project — unchanged except for the removal of your comment — compiles perfectly fine in IntelliJ…

No problems in module-info.java

Since your project uses neither Maven nor Gradle — opting instead to use file-based dependency mgt with the jar in the lib folder — you’re in even worse shape because you’ve eliminated the option of applying any JPMS-enabling plugins that could resolve the issue.

For example, by adding the following pom.xml with the appropriate configuration for the maven-compiler-plugin to my experimental version of your project…

…
<dependencies>
    <dependency>
       <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20200518</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <compilerArgs>
                    <arg>-Xlint:unchecked</arg>
                    <arg>--add-modules</arg>
                    <arg>org.json</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>
…

…Maven does its magic and processes the module-info.java successfully…

No problems in module-info.java

I’ve successfully resolved other Stackers’ JPMS woes by helping them apply that mrJar plugin mentioned in that vscode bug report. So if you’re open to using Gradle instead of Maven, I could likewise advise you on how to configure that plugin too.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement