Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup
Asked Answered
A

13

34

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

Alguire answered 28/11, 2020 at 3:15 Comment(1)
Have you introduced the dependency on org.springframework.spring.context? (about the second error)Decrescent
A
13

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.

Alguire answered 28/11, 2020 at 4:3 Comment(0)
F
4

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.

Fight answered 10/2, 2021 at 21:42 Comment(0)
U
4

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>
Upholster answered 26/6, 2021 at 6:22 Comment(1)
Adding a single dependency to a Spring Boot application that uses a specific version of Spring (not 5.3) is a bad idea and will lerad to other issues.Porpoise
L
2

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.

Loreenlorelei answered 11/12, 2020 at 12:32 Comment(0)
S
1

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>
Scornful answered 10/5, 2021 at 4:8 Comment(0)
C
1

Check if another dependency doesn't have dependency to spring-core with older version

Coefficient answered 5/1, 2023 at 14:53 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Mata
Thanks grzesiu988, your answer helped me. In my case spring-jdbc and spring-webmvc had conflicting spring-core versions. Updating both, such that the same spring-core version was referenced, resolved the issue. In my case it was spirng-webmvc:5.3.27 and spring-jdbc:5.3.27.Minutia
G
0

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.

Goal answered 30/12, 2020 at 21:54 Comment(0)
S
0

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.

Stakeout answered 26/5, 2021 at 8:49 Comment(0)
T
0

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.

Tag answered 14/4, 2022 at 12:6 Comment(0)
P
0

This error happens when various libs from Spring are running different and incompatible versions.

The whole point of Spring Boot (well, one of the points) is that it gives you the dependencyManagement of a whole load of libs, especially Spring itself, that are verified to be mutually compatible.

Therefore, fixing this error by upgrading or downgrading the Spring version may work, but it's the wrong answer because you shouldn't be forcing the version of Spring, you should be letting Spring Boot give you a bunch of version numbers that work together.

Most likely cause of this error is that, in your POM (including any corporate parent POM or other non-Spring Boot imported dependency management), you've specified a version of a Spring library, which you shouldn't specify because it's already specified by Spring Boot. And the version you've specified is incompatible with the other Spring libs brought in via Spring Boot.

The fix is not to change version number in your POM, but to stop specifying the version at all. You can pretty quickly verify that it is managed by Spring Boot, because if you don't specify a version and it's not managed by SB, then Maven will warn you that you haven't specified it.

Note on upgrading SB: Spring Boot today manages many more libraries than it used to in early versions. When you migrate to a newer version of SB, you might find that the versions on some libraries which you previously needed to specify, are no longer needed.

Prepossess answered 2/11, 2023 at 9:30 Comment(0)
C
0
My pom.xml is as below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.newgen</groupId>
    <artifactId>html2pdfUtility</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>html2pdfUtility</name>
    <description>Html to Pdf Utility</description>
    <packaging>jar</packaging>
    <properties>
        <java.version>8</java.version>
        <start-class>com.newgen.html2pdfUtility.Html2pdfUtilityApplication</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.itextpdf/html2pdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>4.0.5</version>
        </dependency>

    </dependencies>

<build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>
                        com.newgen.html2pdfUtility.Html2pdfUtilityApplication</mainClass>
                    <layout>JAR</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <!--<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.newgen.html2pdfUtility.Html2pdfUtilityApplication</mainClass>
          </manifest>
        </archive>
        
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>-->

</project>

After creating jar While running on jboss server getting 
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at com.newgen.html2pdfUtility.Html2pdfUtilityApplication.main(Html2pdfUtilityApplication.java:18)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 1 more
Cymar answered 6/5 at 8:19 Comment(0)
A
-1

May this will help someone, who is facing this error.

=> Error should look like this:

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

at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:229)
at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:112)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:67)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:91)

Solution to the Problem:-

  1. Go to Project folder\ Right click\ Maven\ Update maven project.
  2. Check your Spring version (pom.xml-> view the properties).
  3. Change the Spring version to an older/new version. Otherwise use this:
<properties>
  <maven.compiler.target>1.8</maven.compiler.target>
  <maven.compiler.source>1.8</maven.compiler.source>
  <spring.version>5.2.0.RELEASE</spring.version> <!--add this version-->
</properties>
<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
  </dependency>
</dependencies>
<dependency>
  <groupId>org.springframework</groupId> <!-- sql dependency for jdbc-->
  <artifactId>spring-jdbc</artifactId>
  <version>${spring.version}</version>
</dependency>
  1. After adding above things, Save it(Please wait for the code to build..).
  2. Run your Program
Aetolia answered 11/6, 2022 at 19:14 Comment(1)
He is using Spring Boot, managing individual dependencies or upgrading parts of a dpendency list is a very bad idea.Porpoise
L
-1

I had the same Spring Boot ClassNotFoundException ApplicationStartup issue, the solution was for me to ensure that the spring boot version in my gradle plugin is the same than the dependencies in my build.gradle.kts like:

plugins {
    id("org.springframework.boot") version "2.5.3"
...

}

dependencies {
implementation("org.springframework.boot:spring-boot:2.5.3")
...
}
Laboy answered 12/10, 2022 at 11:9 Comment(3)
The plugin manages the dependencies, there should be no version for the dependencies!Porpoise
"there should be no version for the dependencies!", this statement is irrelevant here, this is the situation I had to face with a legacy project. The question here is not about how to properly setup spring boot with gradle but to solve the ApplicationStartup issue ...Laboy
It isn't irrelevant as when setting versions it mostly means there is or a mixture of versions of different parts of the framework (which will lead to issues) or there is an issue later on when upgrading leading to the first issue.Porpoise

© 2022 - 2024 — McMap. All rights reserved.