java - Spring 启动:运行抛出 ClassNotFoundException

标签 java spring maven spring-boot

我正在尝试完成一些简单的 REST 服务,但是我遇到了 spring-boot 的问题,因为它说 ClassNotFoundException,这里有一些细节:

pom.xml

<?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>it.athirat</groupId>
<artifactId>sww2.1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>it.athirat.controller.Application</start-class>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

Application.java(在src/main/java/it/athirat/controller下)

package it.athirat.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
    }
}

堆栈跟踪:

$ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sww2.1 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) > test-compile @ sww2.1 >>>
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) < test-compile @ sww2.1 <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) @ sww2.1 ---
[WARNING]
java.lang.ClassNotFoundException: it.athirat.controller.Application
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:522)
        at java.lang.Thread.run(Thread.java:748)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.003 s
[INFO] Finished at: 2017-05-22T11:51:49+02:00
[INFO] Final Memory: 17M/226M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) on project sww2.1: An exception occurred while running. it.athirat.controller.Application -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

最佳答案

你可以移除start-class

<start-class>it.athirat.controller.Application</start-class>

只要有 spring boot maven 插件,它就会自己定位 main

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

关于java - Spring 启动:运行抛出 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44110017/

相关文章:

java - 如何使用 Maven 在多模块项目中部署父模块和选定模块

java - 与服务器实例混淆

java - 按 4 的余数对数组进行排序

java - 线程中出现异常 "main"java.rmi.NotBoundException

forms - 表格:checkboxes unchecked by default

windows - Docker-Spring Boot App-Windows上的Hello World

java - 如何以线程安全的方式将递增的用户 ID 分配给新用户?

java - setter方法上使用@Resource注解使用getter方法获取值时返回空指针异常

html - Thymeleaf:如何使用具有可变限制的#numbers.sequence()?

java - Maven:在尝试在线之前如何配置为使用本地存在的 Artifact (~/.m2/repository)?