Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.annotation classes and Java 11 JDK

Tags:

I am migrating from Java 8 to Java 11 and faced the problem. I should use:

  • maven-compiler-plugin 2.5.1 with target 1.8 (compiling WAR in java8)
  • tomcat9
  • Open JDK 11

But on startup gettings constant error:

Post-processing of merged bean definition failed; nested exception is java.lang.NoSuchMethodError: javax.annotation.Resource.lookup()Ljava/lang/String;

I found multiple ways to fix it. Tried to add dependency:

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

tried to add extention:

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

Nothing of these helped.

This is maven-compiler-plugin config:

    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>

Please help to find the solution!!

like image 474
Sviatlana Avatar asked Jul 02 '19 15:07

Sviatlana


People also ask

What is javax annotation API used for?

This class is used to allow multiple resources declarations.

What is javax annotation generated?

The Generated annotation is used to mark source code that has been generated. It can also be used to differentiate user written code from generated code in a single file. When used, the value element must have the name of the code generator.

What is javax annotation resource?

The javax. annotation. Resource annotation is used to declare a reference to a resource; @Resource can decorate a class, a field, or a method.

What is annotation process in Java?

"Annotation Processing" is a hook into the compile process of the java compiler, to analyse the source code for user defined annotations and handle then (by producing compiler errors, compiler warning, emitting source code, byte code ...). API reference: javax. annotation.


2 Answers

When migrating up ahead 3 releases of Java, the first thing one should consider is to update all the major dependencies.

maven-compiler-plugin -> current version is 3.8.1,

2.5.1 is 7 years old.

Try the following to resolve this error:

java.lang.NoSuchMethodError: javax.annotation.Resource.lookup()Ljava/lang/String;

Keep the dependency:

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

And explicitly add it as a module:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <release>11</release>
        <compilerArgs>
            <arg>--add-modules</arg>
            <arg>java.xml.ws.annotation</arg>
        </compilerArgs>
    </configuration>
</plugin>
like image 157
Mikhail Kholodkov Avatar answered Sep 16 '22 14:09

Mikhail Kholodkov


For me the problem was in a conflict between libs: javax.annotations-api^1.3.2 and jsr250-api:1.0. There is a javax.annotation.@Resource annotation in jsr250-api WITHOUT lookup() method! On some running environments jsr250's @Resorse was loaded first, on others - javax.annotations-api's. In the first case my error took place:

Post-processing of merged bean definition failed; nested exception is java.lang.NoSuchMethodError: javax.annotation.Resource.lookup()Ljava/lang/String;

Solving: get rid of one of libs using maven exclusion.

like image 41
Sviatlana Avatar answered Sep 18 '22 14:09

Sviatlana