35

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!!

Sviatlana
  • 1,728
  • 6
  • 27
  • 55
  • 3
    Related to [compiler plugin upgrade](https://stackoverflow.com/questions/49398894/unable-to-compile-simple-java-10-java-11-project-with-maven/51586202#51586202) – Naman Jul 02 '19 at 15:08
  • 1
    If you are using `OpenJDK11` and the accepted answer didn't fix your problem, then try the maven `` solution. `jsr250-api` is included from maven library. That causes this issue. – Diablo Jun 16 '21 at 06:46

5 Answers5

21

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>
Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
  • 1
    thank you for the response, but it didn't help... Updated maven-compiler version to 3.8.0, library javax.annotations 1.3.2 exists in /lib folder inside WAR. Still getting the error NoSuchMethodError: javax.annotation.Resource.lookup()Ljava/lang/String; – Sviatlana Jul 04 '19 at 10:25
  • @Sviatlana In order to reproduce the issue, can you update your question with MCFE (minimal complete verifiable example)? – Mikhail Kholodkov Jul 04 '19 at 11:05
  • 1
    I've just found the solution: there is a conflict between javax.annotations library and jsr250 library... – Sviatlana Jul 04 '19 at 12:13
19

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.

Sviatlana
  • 1,728
  • 6
  • 27
  • 55
7

I had the same issue.

I added these lines, with success:

<build>
...
        <extensions>
            <!-- Prevents this error, with JDK 13: -->
            <!-- NoSuchMethodError: 'java.lang.String javax.annotation.Resource.lookup()' -->
            <extension>
                <groupId>javax.annotation</groupId>
                <artifactId>javax.annotation-api</artifactId>
                <version>1.3.2</version>
            </extension>
            <extension>
                <groupId>javax.annotation</groupId>
                <artifactId>jsr250-api</artifactId>
                <version>1.0</version>
            </extension>
        </extensions>
</build>
etienne-sf
  • 131
  • 1
  • 7
  • This helps only if you don’t use those annotations in your code. And they are not used by libs in project. – Sviatlana Feb 03 '20 at 19:02
  • I have the same issue with `OpenJdk11`. This solution works for me. I see no other side affects so far. – Diablo Jun 16 '21 at 06:44
6

I am migrating from jdk 8 to jdk 13. I faced similar issue. But the thing is since jdk 11 the javax packaged are deprecated and replaced with jakarta libraries. I am using maven 3.8.4 and jdk 13.

   <dependency>
        <groupId>jakarta.annotation</groupId>
        <artifactId>jakarta.annotation-api</artifactId>
        <version>1.3.5</version>
    </dependency>
    
    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
        <version>2.3.2</version>
    </dependency>
Abhishek Singh
  • 532
  • 1
  • 5
  • 16
-3

I had same problem in my project with JDK11.

My solution:

@Autowired
@Qualifier("bean's name")

instead of:

@Resourse
Giancarlo Romeo
  • 663
  • 1
  • 9
  • 24
Alex V
  • 27
  • 1
  • 3
    Autowired is Spring stuff. Not related to this question, only works if you have a Spring app. – eis Feb 17 '21 at 14:15
  • While not specifically related to the question it did help me in my spring app, thanks – Gabe Gates May 02 '22 at 19:17
  • This remove 'Caused by: java.lang.NoSuchMethodError: javax.annotation.Resource.lookup()Ljava/lang/String;' exception, but I get another exception: 'Caused by: java.lang.ClassCastException: class com.sun.proxy.$Proxy88 cannot be cast to class javax.persistence.EntityManagerFactory (com.sun.proxy.$Proxy88 and javax.persistence.EntityManagerFactory are in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @612bd450) ' – victorpacheco3107 Sep 08 '22 at 16:46
  • Agree with @eis Although it **may** help in some particular cases, this is not an answer on topic question. – lospejos Mar 30 '23 at 13:29