Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can be solved java.lang.NoClassDefFoundError: javax/annotation/Generated?

I`ve changed jdk to a 9 version in my project and then collided with an error :

Error:java: java.lang.NoClassDefFoundError: javax/annotation/Generated

I try to solve it by the adding following to pom.com but it wasn`t work for me:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>${version.compiler.plugin}</version>
   <configuration>
       <!-- fork is needed so compiler args can be used -->
       <fork>true</fork>
       <compilerArgs>
           <arg>-J--add-modules</arg>
           <arg>-Jjava.annotations.common</arg>
       </compilerArgs>
   </configuration>
</plugin>

Does it have other way to solve?

like image 840
ziki Avatar asked Jan 13 '18 07:01

ziki


3 Answers

Add an artifact containing the classes you need to the classpath.

It appears that the javax.annotation API is what you need. See https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api/1.3.2 for details. You can add the following dependency to your project as any other and it should be present:

<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

Remember to remove the compiler arguments!

like image 90
Thorbjørn Ravn Andersen Avatar answered Oct 07 '22 14:10

Thorbjørn Ravn Andersen


implementation 'javax.annotation:javax.annotation-api:1.3.2'
annotationProcessor("javax.annotation:javax.annotation-api:1.3.2")
like image 8
askxionghu Avatar answered Oct 07 '22 14:10

askxionghu


From 2020 onwards, the javax.* modules have been transitioned to jakarta. So the 2020+ proof dependency declaration is now:

<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>

See also a blog with explanations and a complete table with old and new names

like image 6
Geert Avatar answered Oct 07 '22 16:10

Geert