'Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup

I am currently playing with some proof-of-concept work in Spring Boot and GCP data storage.

My pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.4.0</version>
</dependency>

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-data-datastore</artifactId>
  <version>1.2.6.RELEASE</version>
</dependency>

Issue: Spring Boot fails to start

When I attempt to launch the application, I get:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:251)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:264)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)

What I tried

I tried adding the Actuator dependency. But that did not do the trick. I cannot figure out what dependency I am missing. I see the class definition here in 5.3.0-M2 documentation, but I'm not sure what dependency it exists in.

I also tried adding following metrics dependencies:

  • spring-cloud-gcp-starter-metrics
  • spring-metrics
  • spring-cloud-stream-metrics

I searched in findjar.com with no luck.

I wouldn't mind disabling it as well if that is possible.


Update:

I added:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.3.1</version>
</dependency>

Which gives me a new error:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

org.springframework.boot.SpringApplication.run(SpringApplication.java:324)

The following method did not exist:

'void org.springframework.context.ConfigurableApplicationContext.setApplicationStartup(org.springframework.core.metrics.ApplicationStartup)'

The method's class, org.springframework.context.ConfigurableApplicationContext, is available from the following locations:

... Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.context.ConfigurableApplicationContext



Solution 1:[1]

I was able to solve this by downgrading Spring Boot:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.3.3.RELEASE</version>
</dependency>

Guess it's just not compatible with 2.4.0 yet.

Specifically I also had to ensure that I used 2.3.3.RELEASE and not anything more recent due to other issues I ran across.

Solution 2:[2]

I don't have the spring-cloud dependency, but when upgrading to Spring Boot 2.4 from 2.3 I got the same error.

Exception in thread "main" java.lang.NoClassDefFoundError:
 org/springframework/core/metrics/ApplicationStartup

Per conversation in this thread (https://github.com/spring-projects/spring-boot/issues/24880) I upgraded to Spring Framework 5.3.3 and it fixed the problem.

Solution 3:[3]

I had a similar issue, and it was resolved by adding the following maven dependency:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.3.8</version>
</dependency>

Solution 4:[4]

I had the same issue caused by an old version of spring-context JAR file loaded instead of the appropriate version.

Ensure that there were no reminiscence of old libraries in your classpath.

Solution 5:[5]

I was having the same issue but with:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.2.9.RELEASE</version>
        <scope>test</scope>
    </dependency>

And the error disappeared after I upgraded to:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.3.3</version>
        <scope>test</scope>
    </dependency>

Solution 6:[6]

May this help, upgrade spring-cloud to the latest version.

This works for me when I faced your first error :

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup

See the twitter thread here, thanks to Stéphane and Arnaud for the help.

Solution 7:[7]

I had the same problem, but that was caused by spring-security-ldap that i also import in version 5.4.x

Upgrade spring-security-ldap to version 5.5.0 solved the problem for me.

Solution 8:[8]

I had this issue when adding a Spring Boot test to a library for the first time. In my case, the library did not have a Spring Boot application.

This did the trick:

@SpringBootApplication
public class TestApplication {
  public static void main(final String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

Using only spring-boot-starter-test as a Spring dependency.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gordon Bean
Solution 3 Vasyl Sarzhynskyi
Solution 4
Solution 5 Carlos Marmolejo
Solution 6 Mohammed
Solution 7 Greg
Solution 8 pyb