尝试保存图像时出现异常

时间:2013-03-11 18:22:56

标签: java jar javax.imageio imagej

启动Java应用程序时,尝试保存图像时会出现异常。然而,在Eclipse中,一切正常。应用程序是使用fatjar构建的,并且还选择了必要的库(jar_imageio.jar和ij.jar)进行导出。

我尝试使用ImageIO和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 个答案:

答案 0 :(得分:2)

正如@Victor所说,我认为你应该看看

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!

我昨天才遇到这个问题,这很棘手。有类似的问题here。我发现如果我在jar中包含了jai_imageio并且没有修改清单文件以包含JAI清单文件的内容或者在你的构建中将META-INF的services文件夹中的文件组合在一起然后我有一些类似于你的错误。我的应用程序确实工作但没有包含JAI,因为JAI安装在本地我选择使用JAI包含暂时构建它。

打开jar,你会发现一个名为META-INF的目录。在那里有文件MANIFEST.MF。我使用Maven将JAI内容包含在Manifest文件中,看起来像

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

您应该在那里替换您的姓名和主要班级。如果你不使用Maven(或Ant)来使它工作,你可以修改这个文件并在命令行上自行设置它。我有一个额外的问题,我的一些包含的jar正在覆盖META-INF的services文件夹中的文件。相反,我使用Maven的Shade插件合并了这些文件。

答案 1 :(得分:0)

将这些行添加到build.xml中(为我解决)

<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>