java.util.ServiceConfigurationError:无法实例化提供程序

时间:2016-08-23 13:40:00

标签: java javax.imageio jai

我有一个.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

2 个答案:

答案 0 :(得分:0)

我运行此代码并创建了一个新的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");
        }

    }
}

答案 1 :(得分:0)

显然,发生了冲突,我使用的是来自不同库的类,在这里我同时拥有jai_imageiojai-imageio-jpeg2000,我只需删除其中一个就解决了这个问题。