Java程序使用Alpine Linux報錯java.lang.NoClassDefFoundError: Could not initialize class org.xerial.snappy.S

報錯內容

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)測試的時候發現有一個功能不正常,爬出了以上重要的日誌java

發現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.solinux

經過這個信息能夠定位出是未找到ld-linux-x86-64.so庫git

解決方式

因爲openjdk:8-jdk-alpine容器使用的是Alpine Linux,經多方查找,能夠使用如下方式解決github

建立軟鏈接,若是/lib目錄下有linux-x86-64.so.2能夠不用執行後邊的操做,沒有則須要繼續安裝
ln -s /lib /lib64web

並安裝多個庫
apk add --no-cache bash tini libc6-compat linux-pam krb5 krb5-libsspring

bash不是必須的,只是用起來方便apache

最後添加到Dockerfile中以下
centos

參考

https://issues.apache.org/jira/browse/SPARK-26995
https://github.com/apache/spark/pull/23898
https://github.com/rluta/spark/commit/f3f3b6353874bd19aa5c720a4a55ccebf29cafb1bash

相關文章
相關標籤/搜索