java.util.ServiceConfigurationError: Provider could not be instantiated

走远了吗. 提交于 2019-12-13 07:23:16

问题


I have a .jp2 image file that I want to convert to .jpg.

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

I have got this exception :

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

回答1:


Apparently, a conflict occured, I was using classes from different libraries, here I had both jai_imageio and jai-imageio-jpeg2000, I solved this problem by simply removing one of them.




回答2:


I run this code and it created a new jpg file. I hope it would help you.

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");
        }

    }
}


来源:https://stackoverflow.com/questions/39102861/java-util-serviceconfigurationerror-provider-could-not-be-instantiated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!