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

11

26

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.

Zampardi answered 1/8, 2018 at 9:56 Comment(4)
Did you clean your project in Eclipse after you changed the settings?Ursal
no, i don't know how, i'm beginner, how to do it?Zampardi
Click Project > Clean... Not certain if it will solve your problem but it does with me 99% of the time when Eclipse doesn't find my imports.Ursal
I did clean it and the Error is still thereZampardi
L
48

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>
Langtry answered 1/8, 2018 at 10:15 Comment(4)
the thing is this project is not using Maven, manual jar download, and for the moment i want to do it that way, any alternative? is this an Eclipse bug or just that Java 10 doesn't ship with javax.annotation?Zampardi
If I understand correct annotations are deprecated from Java 9+ and Spring doesn't see it by default. Maybe you can try install Spring 5.1 (it seems that it can run even with Java 11 - github.com/spring-projects/spring-framework/wiki/…)Langtry
This still caused me problems in Tomcat, so I just had to go back to java 8 and it solved my issues.Roye
Anyone who is trying to resolve similar issue and still faces the issue after importing above dependency, please try to check which version of JDK is selected for project in IDEHimyaritic
M
8

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.

Microstructure answered 27/1, 2021 at 1:10 Comment(0)
F
6

Well This happens Because @PostConstruct and @preDestroy are deprecated in java 9 onwards so better not to use them instead use interface to resolve this

Fda answered 9/11, 2020 at 8:34 Comment(0)
S
5

missing Javax Annotation

https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api/1.3.2

jar or dependency can be found here.

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>
Syncopated answered 28/3, 2021 at 17:37 Comment(0)
K
2

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

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

Kiangsi answered 8/4, 2020 at 10:9 Comment(0)
P
2

This worked for me when I ran into this type of issue. I added this to my pom.xml

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifact Id>
    <version>1.2</version>
  </dependency>
Piggin answered 21/2, 2022 at 19:5 Comment(0)
M
0

You can add the interfaces InitializingBean and DisposableBean to your class. Then you can use the methods:afterPropertiesSet() and destroy().

Maynard answered 20/12, 2021 at 15:3 Comment(0)
N
0

@PreDestroy and @PostConstruct are from

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;t

After importing these two libraries in eclipse if eclipse can not recognize them then you have to add a jar.

javax.annotation-api

Download here.

Add it to your project.

And add it to Reference Libraries.

To add jar, goto Project Properties >> Java Build Path >> Libraries >> Add Jars

Notice answered 15/2, 2022 at 12:7 Comment(0)
L
0

If you are using Gradle, you can add this this line to the dependencies:

implementation 'org.springframework.boot:spring-boot-starter-web'

This is an example of the dependencies that I am using in one project:

dependencies {
    implementation 'com.graphql-java:graphql-java:11.0'
    implementation 'com.graphql-java:graphql-java-spring-boot-starter-webmvc:1.0'
    implementation 'com.google.guava:guava:26.0-jre'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

This should solve the question's problem on Java 10 up to Java 17.

Lippizaner answered 12/4, 2022 at 16:36 Comment(0)
S
0

I found that I had installed a newer version of Java, and needed to use Java 8 for a new project I was on. The solution was to look for references to the new er version of Java on the OS.

I found a reference in my .zshrc file to the newer version of Java, and removing that line fixed the issue for me. Specifically I removed this line:

export JAVA_HOME=$(/usr/libexec/java_home -v 15.0.2+10)
Sabba answered 30/8, 2022 at 20:59 Comment(0)
G
0

This is because @PostConstruct and @PreDestroy are deprecated in java 9. To use them in java 9 or above version, you have to add the following dependency to you pom.xml

    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
Gurney answered 19/12, 2023 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.