我最近开始使用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);
}
}
}
但是当我尝试使用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
。
我做错了什么?
答案 0 :(得分:6)
你的pom.xml中有两件事要做。
首先将start-class更改为您的应用程序类。 其次,将超酷的Spring Boot maven构建器添加到您的pom中。
这样的事情:
<?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
然后使用&#34; mvn install&#34;创建你的jar。你的代码运行得很好。
答案 1 :(得分:1)
应更改以下标记的值以使用main方法匹配实际类。尝试指定正确的包并运行“mvn install”
<start-class>com.sample.Application</start-class>
答案 2 :(得分:0)
您的应用程序在此包中:
package com.spring.app;
这意味着您的应用程序完整路径应该是com.spring.app.Application包,并且您的错误代码表示com.spring.Application
未找到。
也许你应该检查你的运行配置
答案 3 :(得分:0)