Exception when trying to save images

2019-02-20 13:06发布

When starting my Java application, I get exceptions when trying to save images. In Eclipse, however, everything works fine. The application is built using fatjar and the necessary libraries (jar_imageio.jar and ij.jar) have been selected for export as well.

I tried using ImageIO and ImageJ:

a.) ImageIO:

ImageIO.write(image, "jpg", new File(f));


Exception in thread "main" sun.misc.ServiceConfigurationError: 
javax.imageio.spi.ImageWriterSpi: 
Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi 
could not be instantiated: java.lang.IllegalArgumentException: vendorName == null!
    at sun.misc.Service.fail(Unknown Source)
    at sun.misc.Service.access$200(Unknown Source)
    at sun.misc.Service$LazyIterator.next(Unknown Source)
    at javax.imageio.spi.IIORegistry.registerApplicationClasspathSpis(Unknown Source)
    at javax.imageio.spi.IIORegistry.<init>(Unknown Source)
    at javax.imageio.spi.IIORegistry.getDefaultInstance(Unknown Source)
    at javax.imageio.ImageIO.<clinit>(Unknown Source)

b.) ImageJ:

IJ.saveAs(image, "jpg", f);
java.lang.NoClassDefFoundError: Could not initialize class javax.imageio.ImageIO

    at ij.plugin.JpegWriter.saveAsJpeg(JpegWriter.java:49)
    at ij.plugin.JpegWriter.save(JpegWriter.java:28)
    at ij.io.FileSaver.saveAsJpeg(FileSaver.java:340)
    at ij.io.FileSaver.saveAsJpeg(FileSaver.java:332)
    at ij.plugin.filter.Writer.run(Writer.java:24)
    at ij.plugin.filter.PlugInFilterRunner.processOneImage(PlugInFilterRunner.java:256)
    at ij.plugin.filter.PlugInFilterRunner.<init>(PlugInFilterRunner.java:105)
    at ij.IJ.runPlugIn(IJ.java:158)
    at ij.Executer.runCommand(Executer.java:127)
    at ij.Executer.run(Executer.java:64)
    at ij.IJ.run(IJ.java:249)
    at ij.IJ.run(IJ.java:296)
    at ij.IJ.saveAs(IJ.java:1579)

2条回答
混吃等死
2楼-- · 2019-02-20 13:14

add this lines into build.xml (solved for me)

<manifest>
    <attribute name="Main-Class" value="${main.class}"/>
    <attribute name="Built-By" value="${user.name}" />
    <attribute name="Built-Date" value="${TODAY}" />                   
    <attribute name="Implementation-Title" value="MyApp" />
    <attribute name="Implementation-Vendor" value="MyCompany" />                
    <attribute name="Implementation-Version" value="${version.num}-b${build.number}"/>                              
</manifest>
查看更多
小情绪 Triste *
3楼-- · 2019-02-20 13:16

As @Victor says I think you should look at

Exception in thread "main" sun.misc.ServiceConfigurationError: 
javax.imageio.spi.ImageWriterSpi: 
Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi 
could not be instantiated: java.lang.IllegalArgumentException: vendorName == null!

I had this issue just yesterday and it was tricky. There are similar questions here. I found if I included jai_imageio in the jar and did not modify the manifest file to include the contents of the JAI manifest file or combine the files in the services folder of META-INF in your build then I had a number of errors similar to yours. My application did work though without JAI included since JAI was installed locally I opted to build it with JAI included for the time being.

Opening you jar you will find a directory called META-INF. In there is the file MANIFEST.MF. I use Maven to include the JAI things in the Manifest file so it looks like

Manifest-Version: 1.0
Implementation-Title: com.sun.media.imageio
Implementation-Version: 1.0_01
Built-By: myName
Specification-Vendor: Sun Microsystems, Inc.
Created-By: Apache Maven
Implementation-Vendor: Sun Microsystems, Inc.
Build-Jdk: 1.6.0_43
Specification-Title: Java Advanced Imaging Image I/O Tools
Specification-Version: 1.0-mr
Extension-Name: com.sun.media.imageio
Main-Class: myMain
Archiver-Version: Plexus Archiver

You should have your name and your main class substituted in there. You could just modify this file and jar it up yourself on the command line if you don't use Maven (or Ant) to get it working. I had the extra issue where some of my included jars were overwritting files in the services folder of META-INF. Instead I merged these files using Maven's Shade plugin.

查看更多
登录 后发表回答