Intellij: how to add java.annotation module for javax.annotation.PostConstruct

14,024

The trouble with IntelliJ IDEA is that it knows about the module in JDK/jmods/java.xml.ws.annotation.jmod, even though at runtime it will be disabled. Just comment out the java.xml.ws.annotation jmod in the Java SDK definitions in IntelliJ IDEA Project Structure.

Detail steps:

Add a Maven dependency:

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

and module-info dependency:

requires java.annotation;

as you already did.

Then, stop IntelliJ IDEA, go to your IntelliJ JDK config file (i.e. C:\Users\YOUR_NAME\.IntelliJ2018\config\options\jdk.table.xml and comment out the line with java.xml.ws.annotation.jmod reference:

<!-- <root url="jrt://C:/Java-Training/JDK!/java.xml.ws.annotation" type="simple" /> -->

IntelliJ will stop seeing java.xml.ws.annotation. After restart, everything will be fine.

Share:
14,024
Patrick Garner
Author by

Patrick Garner

Updated on July 01, 2022

Comments

  • Patrick Garner
    Patrick Garner almost 2 years

    I upgraded the SDK that my project uses to Java 10. The following import statement caused an error:

    import javax.annotation.PostConstruct;

    Package 'javax.annotation' is declared in module 'java.xml.ws.annotation', but module 'application' does not read it

    enter image description here

    Hitting ALT+ENTER to let Intellij fix it, I received the following options:

    enter image description here

    I selected Add Maven Dependency... and the following dependency was added to pom.xml.

    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
    

    Looking at the newly added JAR's MANIFEST.MF, I noted that the module's name was java.annotation.

    enter image description here

    So i added requires java.annotation to module-info.java.

    module application {
        requires java.annotation;
    }
    

    When I CTRL+clicked on requires java.annotation:

    enter image description here

    Intellij correctly navigated to the JAR:

    enter image description here

    So it appeared that I had the module name correct and that javax.annotation:javax.annotation-api was available to my module. Unfortunately, the error on import javax.annotation.PostConstruct did not go away. To confirm that my module descriptor was working properly, I added other directives, and all worked fine. I also added the directive requires java.xml.ws.annotation,

    enter image description here

    which made the import statement error go away but, of course, this is not a satisfactory solution as java.xml.ws.annotation is deprecated and marked for removal.

    enter image description here

    Any ideas on how to resolve this problem? I need to be able to make module java.annotation available to module application and from what I've read here and here and here and here the way to do it is by adding a JAR to the module path, and from what I've read here the newly added module must be referenced via directive in module-info.java.