Search code examples
javaspringspring-bootmavenspring-mvc

Error: Could not find or load main class com.javacourse.springBootDemo.SpringBootDemoApplication


In Eclipse, when I try to run my SpringBootDemoApplication, it gives me this error:

Error: Could not find or load main class com.javacourse.springBootDemo.SpringBootDemoApplication 
Caused by: java.lang.ClassNotFoundException: com.javacourse.springBootDemo.SpringBootDemoApplication

I have also tried some solutions on here, but non of them worked...

Here are some of the things I've tried:

1. Right Click the project -> Maven -> Update Project -> Then Re-run the project.

- I did it and nothing happened, same error continues.

2. Adding <start-class> property in my pom.xml:

<properties>
    `<start-class>`com.javacourse.springBootDemo.SpringBootDemoApplication</start-class>
</properties>

- Also did nothing. Error still continues.

3. I have checked Run -> Run Configurations... and cleared all then tried recreating.

- Did nothing as well.

4. Checked Window -> Show View -> Problems

- Found nothing.

5. Tried someone's solution which says: "start-class doesn't work for me, I fixed it by adding build plugins to pom.xml, and executions is necessary."

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins> 
</build>

- Did nothing either...

6. Did this one too, nothing happened :

 <build> <pluginManagement> <plugins>
     <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin> </plugins></pluginManagement> 
 </build>

7. Adding the following section in pom didn't help as well...

<plugins>
    <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
    </plugin>
</plugins>

My POM.XML :

<?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>2.6.11</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.javacourse</groupId>
  <artifactId>springBootDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springBootDemo</name>
  <description>Demo project for Spring Boot</description>
  <properties>
      <java.version>11</java.version>
  </properties>
  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

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

  <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <configuration>
                  <executable>true</executable>
              </configuration>
              <executions>
                  <execution>
                      <goals>
                          <goal>repackage</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
      </plugins>
  </build>

</project>

My SpringBootDemoApplication.class :

package com.javacourse.springBootDemo;

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication 
public class SpringBootDemoApplication {

    public static void main(String[] args) {         
    SpringApplication.run(SpringBootDemoApplication.class, args);   

    }

}

My Package Explorer ScreenShot :

Package Explorer

My Maven and Java Versions :

Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: C:\apache-maven-3.8.5
Java version: 11.0.16, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-11.0.16

Thank you all in advance, hope we can solve this vexed issue.


Solution

  • I have finally solved it. To anyone encountering the same issue and couldn't find a solution : Try to change the project folder destination and move it in C:\ directory. I actually don't know the reason yet but I think that maybe it's related to Maven, JDK or Classpath visibility through C:. But that was the only solution for me.