SpringBoot+Java8项目中FFmpegFrameGrabber报NoClassDefFoundError求助
Java8 + SpringBoot 2.7.9 使用JavaCV获取视频时长时触发NoClassDefFoundError
问题详情
尝试通过FFmpegFrameGrabber获取MP4视频时长,创建实例时抛出错误:
java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.ffmpeg.global.avutil
报错代码
private void videoInfo(MultipartFile file) { try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(file.getInputStream())) { // 该行报错 grabber.start(); long durationMs = grabber.getLengthInTime(); } catch (FrameGrabber.Exception e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
当前build.gradle配置
plugins { id 'java' id 'org.springframework.boot' version '2.7.9' id 'io.spring.dependency-management' version '1.0.15.RELEASE' } group = 'com.nob234' version = '0.0.1-SNAPSHOT' configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.springdoc:springdoc-openapi-ui:1.6.9' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'org.postgresql:postgresql' // for logging implementation 'org.slf4j:slf4j-api:1.7.30' implementation 'org.slf4j:jcl-over-slf4j:1.7.30' implementation 'org.slf4j:log4j-over-slf4j:1.7.30' implementation 'ch.qos.logback:logback-classic:1.2.3' implementation 'org.bytedeco:javacv:1.5.8' } tasks.named('test') { useJUnitPlatform() }
本地FFmpeg版本
ffmpeg version 6.0-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers built with gcc 12.2.0 (Rev10, Built by MSYS2 project) configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint libavutil 58. 2.100 / 58. 2.100 libavcodec 60. 3.100 / 60. 3.100 libavformat 60. 3.100 / 60. 3.100 libavdevice 60. 1.100 / 60. 1.100 libavfilter 9. 3.100 / 9. 3.100 libswscale 7. 1.100 / 7. 1.100 libswresample 4. 10.100 / 4. 10.100 libpostproc 57. 1.100 / 57. 1.100
解决方案
1. 补充JavaCV的平台依赖包
仅引入org.bytedeco:javacv不足以加载FFmpeg的本地库,需要添加对应平台的完整依赖。替换原javacv依赖为:
implementation 'org.bytedeco:javacv-platform:1.5.8'
如果需要指定特定平台(如Windows x64),可单独引入对应平台的FFmpeg库:
implementation 'org.bytedeco:javacv:1.5.8' implementation 'org.bytedeco:ffmpeg-platform:5.1.2-1.5.8' // 版本需与javacv严格对应
2. 确认Java版本兼容性
确保项目编译、运行环境均为Java8,避免字节码版本不匹配导致类初始化失败。可在build.gradle中明确指定:
java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 }
3. 排查依赖冲突
执行gradle dependencies查看依赖树,排除与JavaCV/FFmpeg冲突的第三方包。例如存在重复的org.bytedeco相关依赖时,可通过exclude剔除:
implementation('org.bytedeco:javacv-platform:1.5.8') { exclude group: 'xxx', module: 'xxx' // 替换为实际冲突的依赖组和模块 }
4. 调试本地库加载
若问题仍存在,添加JVM参数打印本地库加载日志,定位具体加载失败原因:
-Djava.library.path=path/to/your/ffmpeg/libs -Dorg.bytedeco.javacpp.logger.debug=true
内容的提问来源于stack exchange,提问作者noob234
相关产品推荐
相关产品推荐

