You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Joel示例构建可执行Spring Boot Jar时遇错误求助

Hey there! Let's figure out why your executable Spring Boot JAR build is failing even though the JavaExec task runs smoothly. Here are some targeted checks and fixes to try out:

Common Troubleshooting Steps

1. Verify Spring Boot Plugin Configuration

Whether you're using Gradle or Maven, the build plugin is critical for packaging a proper executable JAR:

  • For Gradle: Make sure the spring-boot-gradle-plugin is applied, and the bootJar task explicitly sets your main class if auto-detection fails:
    plugins {
        id 'org.springframework.boot' version 'your-spring-boot-version'
        id 'java'
    }
    
    bootJar {
        mainClass.set('com.your.package.YourApplicationClass')
    }
    
  • For Maven: Confirm the spring-boot-maven-plugin is included in your build/plugins section with the correct main class specified:
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>your-spring-boot-version</version>
                <configuration>
                    <mainClass>com.your.package.YourApplicationClass</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

2. Check Dependency Packaging Completeness

JavaExec uses your local project's compiled classes and dependencies directly, but the executable JAR needs to bundle all required dependencies (or reference them correctly).:

  • Look for dependencies marked with provided scope — these won't be included in the JAR, but JavaExec might pick them up from your local environment. Adjust the scope if they're needed at runtime.
  • Run ./gradlew dependencies (Gradle) or mvn dependency:tree (Maven) to compare dependencies used by JavaExec vs. what's being packaged into the JAR.

3. Validate Resource/Configuration Paths

Resources like application.properties or application.yml might load fine locally via JavaExec but fail in the JAR:

  • Ensure all configuration files are placed in src/main/resources so they're packaged into the JAR's classpath.
  • Avoid using absolute file paths in your code — use getResourceAsStream() to access resources from the JAR's classpath instead.

4. Dig into Detailed Build Logs

To pinpoint the exact error line:

  • For Gradle: Run ./gradlew bootJar --info to get verbose build logs that show where the process is failing.
  • For Maven: Run mvn package -X to enable debug logging and trace the issue.

5. Confirm Main Class Setup

Double-check that your application's main class has:

  • The @SpringBootApplication annotation.
  • A valid public static void main(String[] args) method that calls SpringApplication.run().

内容的提问来源于stack exchange,提问作者Maka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:12:40