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

Shadow Jar缺失ThinJarWrapper类,启动报错求助

问题

我正在开发一个极简的RestController Spring Boot项目,需要生成两种Jar包:

  • 使用Spring Thin-Launcher构建的瘦Jar包(仅16k,运行java -jar tiny.jar可正常启动服务)
  • 用于AWS Lambda的Shadow Jar包(大小22M,符合预期)

但启动Shadow Jar包时出现报错:

Error: Could not find or load main class org.springframework.boot.loader.wrapper.ThinJarWrapper
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.loader.wrapper.ThinJarWrapper

经确认,Shadow Jar包中未打包启动加载器ThinJarWrapper,附上我的build.gradle配置:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.4'
    id 'io.spring.dependency-management' version '1.1.3'
    id 'com.github.johnrengelman.shadow' version '8.1.1'
    id 'maven-publish'
    id 'org.springframework.boot.experimental.thin-launcher' version "1.0.31.RELEASE"
}

group = 'wirklich.xyz'
version = '1.0.0'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
    mavenLocal()
    maven { url 'https://repo.spring.io/milestone' }
}
	ext {
    set('springCloudVersion', "2023.0.0-M1")
}

assemble.dependsOn = [thinJar, shadowJar]

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
        }
    }
}

shadowJar.mustRunAfter thinJar

import com.github.jengelman.gradle.plugins.shadow.transformers.*

shadowJar {
    archiveClassifier = 'aws'
    manifest {
        inheritFrom(project.tasks.thinJar.manifest)
    }
    // Required for Spring
    mergeServiceFiles()
    append 'META-INF/spring.handlers'
    append 'META-INF/spring.schemas'
    append 'META-INF/spring.tooling'
    append 'META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports'
    append 'META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports'
    transform(PropertiesFileTransformer) {
        paths = ['META-INF/spring.factories']
        mergeStrategy = "append"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.cloud:spring-cloud-function-adapter-aws'
    implementation 'org.springframework.cloud:spring-cloud-function-context'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'com.amazonaws.serverless:aws-serverless-java-container-springboot3:2.0.0-M2'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

tasks.named('test') {
    useJUnitPlatform()
}
解决方案

问题核心在于Shadow Jar继承了Thin Jar的Manifest,导致主类指向ThinJarWrapper,但Shadow Jar是包含所有依赖的胖包,不需要Thin的轻量化启动器,且Thin相关依赖默认不会被打包进Shadow Jar,因此出现类找不到的错误。

修改build.gradle的关键配置:

  1. 修正Shadow Jar的Manifest配置
    取消继承Thin Jar的Manifest,改为设置适合AWS Lambda的主类:
  • 如果是普通Spring Boot应用,主类用Spring Boot 3.x默认的org.springframework.boot.loader.JarLauncher
  • 如果是你的项目主类(带@SpringBootApplication注解的类),直接填写全类名,比如wirklich.xyz.Application

修改后的shadowJar配置:

shadowJar {
    archiveClassifier = 'aws'
    manifest {
        // 替换为你的项目主类或Spring Boot默认启动类
        attributes 'Main-Class': 'wirklich.xyz.Application'
        // 或者使用Spring Boot官方启动器:
        // attributes 'Main-Class': 'org.springframework.boot.loader.JarLauncher'
    }
    // 保留原有的Spring相关合并配置
    mergeServiceFiles()
    append 'META-INF/spring.handlers'
    append 'META-INF/spring.schemas'
    append 'META-INF/spring.tooling'
    append 'META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports'
    append 'META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports'
    transform(PropertiesFileTransformer) {
        paths = ['META-INF/spring.factories']
        mergeStrategy = "append"
    }
}
  1. 额外优化(可选)
    由于Shadow Jar是胖包,完全不需要Thin Launcher的依赖,可以在dependencies中排除Thin相关组件,避免冗余:
dependencies {
    // 原有依赖保持不变,新增排除配置
    implementation('org.springframework.boot.experimental:spring-boot-thin-launcher') {
        exclude group: 'org.springframework.boot.experimental'
    }
}

修改完成后重新执行gradle shadowJar构建,运行生成的Shadow Jar即可正常启动。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 02:52:09