java - Intellij : how to add java. javax.annotation.PostConstruct 注释模块

标签 java intellij-idea module java-9 java-platform-module-system

我将项目使用的 SDK 升级到 Java 10。以下导入语句导致错误:

导入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

按 ALT+ENTER 让 Intellij 修复它,我收到以下选项:

enter image description here

我选择了 Add Maven Dependency... 并将以下依赖项添加到 pom.xml。

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

查看新添加的 JAR 的 MANIFEST.MF,我注意到该模块的名称是 java.annotation。

enter image description here

所以我添加了 requires java.annotation 到 module-info.java。

module application {
    requires java.annotation;
}

当我按住 CTRL 并单击 requires java.annotation 时:

enter image description here

Intellij 正确导航到 JAR:

enter image description here

看来我的模块名称是正确的,而且 javax.annotation:javax.annotation-api 可用于我的模块。不幸的是,import javax.annotation.PostConstruct 上的错误并没有消失。为了确认我的模块描述符是否正常工作,我添加了其他指令,并且一切正常。我还添加了指令 requires java.xml.ws.annotation,

enter image description here

这使得导入语句错误消失了,但是,当然,这不是一个令人满意的解决方案,因为 java.xml.ws.annotation 已被弃用并标记为删除。

enter image description here

关于如何解决这个问题有什么想法吗?我需要能够使模块 java.annotation 可用于模块 application 以及我读过的内容 herehereherehere方法是在模块路径中添加一个 JAR,从我读过的内容来看 here必须通过 module-info.java 中的指令引用新添加的模块。

最佳答案

IntelliJ IDEA 的问题在于它知道 JDK/jmods/java.xml.ws.annotation.jmod 中的模块,即使在运行时它会被禁用。 只需在 IntelliJ IDEA 项目结构中的 Java SDK 定义中注释掉 java.xml.ws.annotation jmod。

详细步骤:

添加 Maven 依赖项:

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

module-info依赖:

requires java.annotation;

就像你已经做过的那样。

然后,停止 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 将不再看到 java.xml.ws.annotation。 重启后一切正常。

关于java - Intellij : how to add java. javax.annotation.PostConstruct 注释模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52084698/

相关文章:

由于找不到类,spring 无法在 tomcat 中启动

javascript - 直接导入 ES6 模块元素还是在导入后解构 const 赋值?

Erlang:模块属性

java - 获取 Maven exec :java plugin to use project module dependencies

java - Java中如何将扫描后的字符串保留在文件中?

java - TestNG 中何时生成报告?

java.lang.ClassNotFoundException sun.jdbc.odbc.JdbcOdbcDriver 在 sql server 2008 与 java 8

java - Jmeter采样器看不到资源文件

Java:查找某个单词在字符串中出现的次数(是否有类似于 C# 的表达式)?

intellij-idea - 我应该将 .cvsignore 文件添加到 CVS 吗?