Java spring-boot 应用程序无法启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26534879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:43:30  来源:igfitidea点击:

spring-boot application fails to start

javaspringspring-mvcspring-boot

提问by Kleber Mota

I recently start to develop a web application with spring-boot, anf, following the guide in the offical site, manage to create this two files:

我最近开始用spring-boot,anf开发一个web应用,按照官网的指导,成功创建了这两个文件:

pom.xml

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>spring</groupId>
  <artifactId>app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <start-class>com.spring.app.Application</start-class>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

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

</project>

Application.java

Application.java

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

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

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

}

but when I try run the application with java -jar appnamei get the error: Cannot find the main class: com.spring.app.Application. Program will exit, and in the terminal: Exception in thread "main" java.lang.NoClassDefFoundError: com/spring/app/Application.

但是当我尝试运行应用程序时,java -jar appname我收到错误:Cannot find the main class: com.spring.app.Application. Program will exit,并在终端中:Exception in thread "main" java.lang.NoClassDefFoundError: com/spring/app/Application

What I am doing wrong?

我做错了什么?

采纳答案by SteveS

You have 2 things to do in your pom.xml.

在 pom.xml 中有两件事要做。

First change the start-class to your application class. Second add the super-cool Spring Boot maven builder to your pom.

首先将开始类更改为您的应用程序类。其次将超酷的 Spring Boot maven builder 添加到你的 pom.xml 文件中。

Something like this:

像这样的东西:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.sample</groupId>
<artifactId>beanlist</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <start-class>com.sample.Application</start-class>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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

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

</project

Then use "mvn install" to create your jar. Your code runs just fine.

然后使用“mvn install”来创建你的jar。你的代码运行得很好。

回答by Oleksandr Chalyi

Value of the following tag should be changed to match real class with main method. Try to specify correct package and run "mvn install"

应更改以下标记的值以匹配真实类与 main 方法。尝试指定正确的包并运行“mvn install”

<start-class>com.sample.Application</start-class>

回答by lbt05

you Application is in this package: package com.spring.app; which means your Application full path should be package com.spring.app.Application and your error code said com.spring.Applicationnot found. Maybe you should check your run configuration

你的应用程序在这个包中: package com.spring.app; 这意味着你的应用程序完整路径应该是包 com.spring.app.Application 并且你的错误代码说com.spring.Application没有找到。也许你应该检查你的运行配置

回答by abhishek prasad

I was also getting same error. Its got resolved by converting the project to maven in eclipse as mentioned in below image:enter image description here

我也遇到了同样的错误。它通过将项目转换为 Eclipse 中的 maven 来解决,如下图所示:在此处输入图片说明

Hope it will you. :)

希望你会。:)