尝试保存图像时出现异常 [英] Exception when trying to save images

查看:503
本文介绍了尝试保存图像时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我尝试使用ImageIO和ImageJ:

I tried using ImageIO and ImageJ:

a。)ImageIO:

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:

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)


推荐答案

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

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!

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

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.

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

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

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

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.

这篇关于尝试保存图像时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆