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

蓝咒 提交于 2019-12-11 01:23:56

问题


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

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

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.

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

module application {
    requires java.annotation;
}

When I CTRL+clicked on requires java.annotation:

Intellij correctly navigated to the JAR:

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,

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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/52084698/intellij-how-to-add-java-annotation-module-for-javax-annotation-postconstruct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!