[英]Intellij: how to add java.annotation module for javax.annotation.PostConstruct
I upgraded the SDK that my project uses to Java 10. The following import statement caused an error: 我将项目使用的SDK升级到Java10。以下import语句导致错误:
import javax.annotation.PostConstruct; 导入javax.annotation.PostConstruct;
Package 'javax.annotation' is declared in module 'java.xml.ws.annotation', but module 'application' does not read it
包“ javax.annotation”在模块“ java.xml.ws.annotation”中声明,但模块“ application”不读取它
Hitting ALT+ENTER to let Intellij fix it, I received the following options: 击中ALT + ENTER让Intellij对其进行修复,我收到以下选项:
I selected Add Maven Dependency...
and the following dependency was added to pom.xml. 我选择了“
Add Maven Dependency...
,并将以下依赖项添加到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. 在查看新添加的JAR的MANIFEST.MF时,我注意到该模块的名称为java.annotation。
So i added requires java.annotation
to module-info.java. 所以我在module-info.java中添加了
requires java.annotation
的内容。
module application {
requires java.annotation;
}
When I CTRL+clicked on requires java.annotation
: 当我CTRL +单击时
requires java.annotation
:
Intellij correctly navigated to the JAR: Intellij正确导航到JAR:
So it appeared that I had the module name correct and that javax.annotation:javax.annotation-api
was available to my module. 看来我的模块名称正确,并且我的模块可以使用
javax.annotation:javax.annotation-api
。 Unfortunately, the error on import javax.annotation.PostConstruct
did not go away. 不幸的是,
import javax.annotation.PostConstruct
的错误没有消失。 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
, 我还添加了伪指令
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. 这使import语句错误消失了,但是,由于java.xml.ws.annotation已过时并标记为删除,因此,这当然不是令人满意的解决方案。
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. 我需要能够使模块
java.annotation
可用于模块application
以及从这里 , 此处 , 此处和此处所读内容中获得的方法是通过向模块路径中添加JAR以及从我所获得的内容中在此处阅读,必须通过module-info.java中的指令引用新添加的模块。
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. IntelliJ IDEA的问题在于它知道
JDK/jmods/java.xml.ws.annotation.jmod
的模块,即使在运行时它将被禁用。 Just comment out the java.xml.ws.annotation
jmod in the Java SDK definitions in IntelliJ IDEA Project Structure. 只需在IntelliJ IDEA项目结构的Java SDK定义中注释掉
java.xml.ws.annotation
jmod。
Add a Maven dependency: 添加一个Maven依赖项:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
and module-info
dependency: 和
module-info
依赖关系:
requires java.annotation;
as you already did. 就像您已经做的那样。
Then, stop IntelliJ IDEA, go to your IntelliJ JDK config file (ie C:\\Users\\YOUR_NAME\\.IntelliJ2018\\config\\options\\jdk.table.xml
and comment out the line with java.xml.ws.annotation.jmod
reference: 然后,停止IntelliJ IDEA,转到IntelliJ JDK配置文件(即
C:\\Users\\YOUR_NAME\\.IntelliJ2018\\config\\options\\jdk.table.xml
并用java.xml.ws.annotation.jmod
参考注释掉该行:
<!-- <root url="jrt://C:/Java-Training/JDK!/java.xml.ws.annotation" type="simple" /> -->
IntelliJ will stop seeing java.xml.ws.annotation
. IntelliJ将停止看到
java.xml.ws.annotation
。 After restart, everything will be fine. 重新启动后,一切都会好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.