Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Previously IF I set my Eclipse to:

  • Java Compiler (JDK Complience) : 1.8
  • Java Build Path (JRE System Libraries) : 1.8

THEN IF i use following Spring code

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

Everything works fine. No error at all.

However, IF I set my Eclipse to:

  • Java Compiler (JDK Complience) : 10
  • Java Build Path (JRE System Libraries) : 10

Now, the import statement is throwing an Error message:

The import javax.annotation.PostConstruct cannot be resolved

and this error also happen on @PreDestroy annotation too.

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?

Thank You.

like image 202
xcode Avatar asked Aug 01 '18 09:08

xcode


2 Answers

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

<dependency>
  <groupId>javax.annotation</groupId>
  <artifactId>javax.annotation-api</artifactId>
  <version>1.3.2</version>
</dependency>
like image 53
Anna van den Akker Avatar answered Sep 20 '22 12:09

Anna van den Akker


You would need to point the Eclipse to Java 8 via Window -->Preferences-->Java and add JDK 1.8 bin path. Once done, project will be built automatically and issue should be resolved.

like image 45
Anvita Shukla Avatar answered Sep 17 '22 12:09

Anvita Shukla