jar包运行报错:找不到主类ClassNotFoundException: DemoApplication

116 阅读1分钟

Exception in thread "main" java.lang.ClassNotFoundException: DemoApplication

Bug Description

在运行SpringBoot项目时,出现如下异常:

Exception in thread "main" java.lang.ClassNotFoundException: DemoApplication

Reproduciton Steps

1.maven打包项目,生成app.jar。依赖如下:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <includeSystemScope>true</includeSystemScope>
            <mainClass>DemoApplication</mainClass>
        </configuration>
    </plugin>
</plugins>

2.上传服务器,执行如下命令:

java -jar app.jar

Reason

1.在pom.xml文件中,<mainClass>标签中的类名是错误的,需要将包路径和类名写全。

Solution

修改configuration下的mainClass为:

<configuration>
    <includeSystemScope>true</includeSystemScope>
    <mainClass>com.example.demo.DemoApplication</mainClass>
</configuration>