Java程序使用Alpine Linux报错java.lang.NoClassDefFoundError: Could not initialize class org.xerial.snappy.Snappy解决

时间:2021-06-01 17:00:23

报错内容

Caused by: java.lang.UnsatisfiedLinkError: /tmp/snappy-1.1.7-4a4b576a-c34c-481e-b6ac-9b4abacb112e-libsnappyjava.so:
Error loading shared library ld-linux-x86-64.so.2:
No such file or directory (needed by /tmp/snappy-1.1.7-4a4b576a-c34c-481e-b6ac-9b4abacb112e-libsnappyjava.so) nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.xerial.snappy.Snappy
org.springframework.web.util.NestedServletException: Handler dispatch failed;
nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.xerial.snappy.Snappy

描述

由于项目中使用了org.xerial.snappy.Snappy这个类,在正常的centos系统环境下,没有问题;在微服务容器(openjdk:8-jdk-alpine)测试的时候发现有一个功能不正常,爬出了以上重要的日志

发现org.xerial.snappy.Snappy这个类无法初始化,日志往上查发现了真实的情况,即java.lang.UnsatisfiedLinkError: /tmp/snappy-1.1.7-4a4b576a-c34c-481e-b6ac-9b4abacb112e-libsnappyjava.so: Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /tmp/snappy-1.1.7-4a4b576a-c34c-481e-b6ac-9b4abacb112e-libsnappyjava.so

通过这个信息可以定位出是未找到ld-linux-x86-64.so库

解决方式

由于openjdk:8-jdk-alpine容器使用的是Alpine Linux,经多方查找,可以使用以下方式解决

创建软连接,如果/lib目录下有linux-x86-64.so.2可以不用执行后边的操作,没有则需要继续安装

ln -s /lib /lib64

并安装多个库

apk add --no-cache bash tini libc6-compat linux-pam krb5 krb5-libs

bash不是必须的,只是用起来方便

最后添加到Dockerfile中如下

Java程序使用Alpine Linux报错java.lang.NoClassDefFoundError: Could not initialize class org.xerial.snappy.Snappy解决

参考

https://issues.apache.org/jira/browse/SPARK-26995

https://github.com/apache/spark/pull/23898

https://github.com/rluta/spark/commit/f3f3b6353874bd19aa5c720a4a55ccebf29cafb1