user3408531
user3408531

Reputation:

java.util.ServiceConfigurationError: Provider could not be instantiated

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

Upvotes: 0

Views: 8125

Answers (2)

user3408531
user3408531

Reputation:

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.

Upvotes: 1

erolkaya84
erolkaya84

Reputation: 1849

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

    }
}

Upvotes: 0

Related Questions