【问题标题】:Spring Boot Hello World Example Not workingSpring Boot Hello World 示例不工作
【发布时间】:2020-01-08 15:30:13
【问题描述】:

我已经从https://start.spring.io/下载了spring boot示例。

我尝试了很多次,但我一次又一次地得到同样的错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
    at com.example.demo.Application.main(Application.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication

这是我的 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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>SpringBoot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</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-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

和 SpringBootClass

package com.example.demo;

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

@SpringBootApplication
public class Application {

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

}

也是我的 ControllerClass

package com.example.demo.controller;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class HelloWorldController {

@RequestMapping("/hello")
@ResponseBody
public String sayHello() {
return "Hello World Developer!!!";
}
}

过去三天我一直在苦苦挣扎,但没有得到任何帮助。 我正在使用 Spring 工具套件 我是 Spring Boot 新手,在这里需要帮助

【问题讨论】:

  • 尝试移除这个依赖spring-boot-starter-tomcat
  • 如何运行应用程序?
  • 我只是在 STS 中将应用程序作为 SpringBoootApp 运行,或者单击主类中的绿色图标

标签: java spring-boot spring-mvc


【解决方案1】:

将 Maven 设置为您的路径并使用命令从命令提示符/终端尝试: mvn 干净安装 这将在目标文件夹中创建 jar 文件。 转到目标文件夹并运行 java -jar jarName.jar。

尝试从项目选项卡中清理项目, 将所有 Maven 依赖项添加到 Eclipse 构建路径。 然后尝试将其作为 java 应用程序运行。

【讨论】:

    【解决方案2】:

    这是由损坏的 Maven 存储库引起的。

    删除C:\Users\&lt;me&gt;\.m2\repository下的所有内容。

    然后做 Eclipse Maven 更新,它会工作的。

    就像spring-boot.jar 被破坏了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      • 2013-10-12
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多