首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-boot应用程序启动失败

spring-boot应用程序启动失败
EN

Stack Overflow用户
提问于 2014-10-23 18:30:24
回答 4查看 55.7K关注 0票数 3

最近,我开始开发一个带有spring-boot,anf的web应用程序,按照官方网站中的指南,设法创建这两个文件:

pom.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
@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);
        }
    }

}

但是,当我尝试使用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

我做错什么了?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-10-23 18:50:26

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

首先,将start类更改为应用程序类。其次,将超级酷的构建器添加到pom中。

就像这样:

代码语言:javascript
复制
<?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

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

票数 6
EN

Stack Overflow用户

发布于 2014-10-23 19:31:23

以下标记的值应更改为将real类与main方法匹配。尝试指定正确的包并运行"mvn安装“。

代码语言:javascript
复制
<start-class>com.sample.Application</start-class>
票数 1
EN

Stack Overflow用户

发布于 2017-03-12 22:23:07

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26534879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档