java.util.ServiceConfigurationError: Provider could not be instantiated

java.util.ServiceConfigurationError: Provider could not be instantiated

我有一个 .jp2 图像文件,我想将其转换为 .jpg

    BufferedImage background = ImageIO.read(new File("images\"
    + randNum + ".jp2"));
    ImageIO.write(background, "jpg", new File("images\" + randNum
                + ".jpg"));

我遇到了这个异常:

java.util.ServiceConfigurationError: javax.imageio.spi.ImageWriterSpi:  Provider com.github.jaiimageio.jpeg2000.impl.J2KImageWriterSpi could not be instantiated
 ...
Caused by: java.lang.NoClassDefFoundError: com/github/jaiimageio/impl/common/PackageUtil
 ...
Caused by: java.lang.ClassNotFoundException: com.github.jaiimageio.impl.common.PackageUtil

我 运行 这段代码,它创建了一个新的 jpg 文件。我希望它能帮助你。

package yourPackage;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class ImageConverter {


    public static void main(String[] args) throws IOException {
        int randNum = 1;
        convertImage(randNum);      

    }

    private static void convertImage(int randNum) throws IOException {
        try {
            File foundFile = new File("c:\images\" + randNum + ".jp2");   
            BufferedImage background = ImageIO.read(foundFile);
            ImageIO.write(background, "jpg", new File("c:\images\" + randNum + ".jpg"));
            System.out.println("jpg file is generated");
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("No file " + randNum +".jp2 found");
        }

    }
}

显然,发生了冲突,我使用的是来自不同库的 类,这里我有 jai_imageiojai-imageio-jpeg2000,我通过简单地删除其中一个来解决了这个问题。