0

I've created a SpringBoot application and ran into this when trying to launch it:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/env/ConfigurableEnvironment
at com.example.DemoApplication.main(DemoApplication.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.env.ConfigurableEnvironment
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

I understand it means ConfigurableEnvironment wasn't found in the classpath or the jar containing it not in the classpath. However I doubt this is even the case. I looked up the doc and knew this class is from Spring-core since 3.1, mine in the classpath is 4.3.3, and I double checked like below:

 jar tvf spring-core-4.3.3.RELEASE.jar | grep ConfigurableEnvironment
 732 Mon Sep 19 14:50:30 EDT 2016  org/springframework/core/env/ConfigurableEnvironment.class

I looked up google and came across this web tool, Spring initilizer and I used it to create the most simple and unpolluted SpringBootApplication, still, it gives me the same error.

I admit I am at my wit end, anyone kindly advice please.

J.E.Y
  • 1,173
  • 2
  • 15
  • 37
  • How do you execute your code? The most obvious thing would it be that the jar is not at the classpath. – mh-dev Oct 05 '16 at 03:55
  • I used mvn goal dependency:build-classpath and eclipse: eclipse to create .classpath and .project and import it into eclipse and run the application class containing the main(); I looked into the classpath and spring-core.4. 3.3.RELEASE was there for sure. Another way I tried was from command line run maven goal clean springboot: run, same error in both cases – J.E.Y Oct 05 '16 at 04:02
  • Ok, this is weird. To be honest at this point I would create a new project via the initializer unpack it und just run it. If it does not start. Something with the environment is not correct. – mh-dev Oct 05 '16 at 04:22
  • This one can help you with this problem. http://stackoverflow.com/questions/39675845/classnotfoundexception-for-included-dependency/39687000#39687000 – Moshe Arad Oct 05 '16 at 05:20
  • check for same jars with different versions and if exists remove one keep one – Prasanna Kumar H A Oct 05 '16 at 05:34

2 Answers2

1

If you are using start.spring.io and the application does not start that way, your local maven cache is corrupted. Try to remove the jar file from ~/.m2/repository so that you can download it again.

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
0

finally I did two things to make it work:

  1. I remove the whole org/springframework under .m2/respository;

  2. In my pom.xml I used to have bom dependencies:

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-framework-bom</artifactId>
      <version>4.1.6.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    

and I replaced it with

<dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.4.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

From my very limited undestanding about bom is that it can help manage the versions of each spring (or most) dependencies, so we don't have to specify their individual version number. Now seems that it didn't properly functioning when spring boot is applied, instead we have to replace it with the artifact spring-boot-dependencies.

J.E.Y
  • 1,173
  • 2
  • 15
  • 37