简体   繁体   English

spring-boot应用程序无法启动

[英]spring-boot application fails to start

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开发一个web应用程序,anf,按照官方网站上的指南,设法创建这两个文件:

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

@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 appname i get the error: Cannot find the main class: com.spring.app.Application. Program will exit 但是当我尝试用java -jar appname运行应用程序时,我得到错误: Cannot find the main class: com.spring.app.Application. Program will exit 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 . 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? 我做错了什么?

You have 2 things to do in your pom.xml. 你的pom.xml中有2件事要做。

First change the start-class to your application class. 首先将start-class更改为您的应用程序类。 Second add the super-cool Spring Boot maven builder to your pom. 第二,将超酷的Spring Boot maven builder添加到你的pom中。

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. 然后使用“mvn install”创建你的jar。 Your code runs just fine. 你的代码运行得很好。

Value of the following tag should be changed to match real class with main method. 应更改以下标记的值以使用main方法匹配实际类。 Try to specify correct package and run "mvn install" 尝试指定正确的包并运行“mvn install”

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

you Application is in this package: package com.spring.app; 你的应用程序在这个包中: package com.spring.app; which means your Application full path should be package com.spring.app.Application and your error code said com.spring.Application not found. 这意味着您的应用程序完整路径应该是com.spring.app.Application包,您的错误代码表示com.spring.Application Maybe you should check your run configuration 也许你应该检查你的运行配置

I was also getting same error. 我也得到同样的错误。 Its got resolved by converting the project to maven in eclipse as mentioned in below image: 通过在eclipse中将项目转换为maven来解决它,如下图所示: 在此输入图像描述

Hope it will you. 希望你能。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果枚举验证失败,则阻止启动 spring-boot 应用程序 - Prevent to start spring-boot application if enum valitiation fails Spring-boot:应用程序无法启动 - Spring-boot : Application could not start 春季启动应用程序:ApplicationContextException:无法启动EmbeddedWebApplicationContext - spring-boot application: ApplicationContextException: Unable to start EmbeddedWebApplicationContext 无法在IntelliJ Idea中启动spring-boot应用程序 - Unable to start spring-boot application in IntelliJ Idea 是否可以以编程方式启动外部 spring-boot 应用程序 - Is it possible to start up an external spring-boot application programmatically 是否可以启动嵌入在我的 spring-boot 应用程序中的 Axon 服务器? - Is it possible to start the Axon server embedded in my spring-boot application? 如何通过命令行启动 spring-boot 应用程序? - How to start up spring-boot application via command line? 升级到 Spring Boot 2 后应用程序无法启动 - Application fails to start after upgrading to Spring Boot 2 Spring 引导自动配置应用程序启动失败 - Spring boot autoconfigure application start fails spring-boot web 应用程序启动失败:由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext - spring-boot web app fails to start : Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM