import javax.annotation.* 无法在 Eclipse 的 Java 10 编译器中解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51630397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:11:23  来源:igfitidea点击:

import javax.annotation.* cannot be resolved in Eclipse's Java 10 Compiler

javaspringeclipsespring-mvc

提问by xcode

In my machine (Windows 10), there are two versions of Java, Java 1.8 (JRE and JDK) and Java 10 (JRE and JDK).

在我的机器(Windows 10)中,有两个版本的 Java,Java 1.8(JRE 和 JDK)和 Java 10(JRE 和 JDK)。

Previously IFI set my Eclipseto:

以前,如果我将Eclipse设置为:

  • Java Compiler (JDK Complience) : 1.8
  • Java Build Path (JRE System Libraries) : 1.8
  • Java 编译器(JDK 合规性):1.8
  • Java 构建路径(JRE 系统库):1.8

THEN IFi use following Spring code

那么如果我使用以下 Spring 代码

import javax.annotation.PostConstruct;
...
...
...
@PostConstruct
...
...

Everything works fine. No error at all.

一切正常。完全没有错误。

However, IF I set my Eclipse to:

但是,如果我将 Eclipse 设置为:

  • Java Compiler (JDK Complience) : 10
  • Java Build Path (JRE System Libraries) : 10
  • Java 编译器(JDK 合规性):10
  • Java 构建路径(JRE 系统库):10

Now, the importstatement is throwing an Error message:

现在,该import语句抛出错误消息:

The import javax.annotation.PostConstruct cannot be resolved

and this error also happenon @PreDestroyannotation too.

这个错误也发生@PreDestroy注释上。

Why is this happening? What happen to Java 10? How to solve this problem if i still want to retain Java Compiler and JRE System Libraries version to Java 10?

为什么会这样?Java 10 会发生什么?如果我仍想将 Java Compiler 和 JRE System Libraries 版本保留到 Java 10,如何解决此问题?

Thank You.

谢谢你。

回答by Anna van den Akker

You can try to add annotation dependencies to pom.xml, so that they would be available for Spring:

您可以尝试向 pom.xml 添加注释依赖项,以便它们可用于 Spring:

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

回答by dcba dcba

Add javax.annotation-api-1.3.2.jarto your classpath. It works with JDK 10 as well.
For more clarity on alternative approaches, go through below video link:

添加javax.annotation-api-1.3.2.jar到您的类路径。它也适用于 JDK 10。
为了更清楚地了解替代方法,请查看以下视频链接:

https://www.youtube.com/watch?v=jOYSRwwMLX8&list=PLzS3AYzXBoj9IBdtgXRSyZEwlU2QV-mGG&index=6

https://www.youtube.com/watch?v=jOYSRwwMLX8&list=PLzS3AYzXBoj9IBdtgXRSyZEwlU2QV-mGG&index=6