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
spring-boot application fails to start
提问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 appname
i 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.Application
not found.
Maybe you should check your run configuration
你的应用程序在这个包中:
package com.spring.app;
这意味着你的应用程序完整路径应该是包 com.spring.app.Application 并且你的错误代码说com.spring.Application
没有找到。也许你应该检查你的运行配置