Intellij:如何为javax.annotation.PostConstruct添加java.annotation模块 [英] Intellij: how to add java.annotation module for javax.annotation.PostConstruct

查看:3219
本文介绍了Intellij:如何为javax.annotation.PostConstruct添加java.annotation模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

import javax.annotation.PostConstruct;

import javax.annotation.PostConstruct;

在模块'java.xml.ws.annotation'中声明了包'javax.annotation',但是模块'application'无法读取

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

点击ALT + ENTER让Intellij对其进行修复,我收到了以下选项:

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

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

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>

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

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

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

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

module application {
    requires java.annotation;
}

当我CTRL +单击requires java.annotation时:

When I CTRL+clicked on requires java.annotation:

Intellij正确导航到JAR:

Intellij correctly navigated to the JAR:

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

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,

使得import语句错误消失了,但是,当然,这不是一个令人满意的解决方案,因为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.

关于如何解决此问题的任何想法?我需要能够使模块java.annotation可用于模块application,并且从此处阅读到的内容中此处此处此处做到这一点的方法是,将JAR添加到模块路径中,并从我阅读的内容中<在href ="https://www.oracle.com/corporate/features/understanding-java-9-modules.html" rel ="nofollow noreferrer">此处必须通过中的指令引用新添加的模块module-info.java.

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.

推荐答案

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

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.

添加Maven依赖项:

Add a Maven dependency:

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

module-info依赖项:

requires java.annotation;

就像您已经做过的那样.

as you already did.

然后,停止IntelliJ IDEA,转到IntelliJ JDK配置文件(即C:\Users\YOUR_NAME\.IntelliJ2018\config\options\jdk.table.xml,并用java.xml.ws.annotation.jmod reference注释掉该行:

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将停止显示java.xml.ws.annotation. 重新启动后,一切都会好起来.

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

这篇关于Intellij:如何为javax.annotation.PostConstruct添加java.annotation模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆