在做二维码生成时,本地开发没有出问题,而上到服务器出现如下错误:
ERRO: java.lang.NoClassDefFoundError: Could not initialize class com.sun.imageio.plugins.jpeg.JPEGImageReader java.lang.NoClassDefFoundError: Could not initialize class com.sun.imageio.plugins.jpeg.JPEGImageReader at com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi.createReaderInstance(JPEGImageReaderSpi.java:85) at javax.imageio.spi.ImageReaderSpi.createReaderInstance(ImageReaderSpi.java:320) at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:529) at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:513) at javax.imageio.ImageIO.read(ImageIO.java:1443) at javax.imageio.ImageIO.read(ImageIO.java:1352)
其关键代码如下:
public static File writeFile(BufferedImage image, String outPutPath) throws IOException { File outPutImage = new File(outPutPath); //如果图片不存在创建图片 if (!outPutImage.exists()) { outPutImage.createNewFile(); } //将二维码写入图片 ImageIO.write(image, FORMAT, outPutImage); return outPutImage; }
使用的JDK为:
openjdk version “1.8.0_265”
由于之前遇到过类似的情况,这次通过添加启动参数进行强制指定工具。
-Djava.awt.headless=true -Dawt.toolkit=sun.awt.HToolkit
重新启动,报错信息如下:
libjpeg.so.8: cannot open shared object file: No such file or directory
原来是lib包缺失。
解决办法:
apt -y install libjpeg-turbo8-dev
最后重启,解决问题。