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

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

问题描述

在启动我的 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 的服务文件夹中的文件,那么我有许多与您类似的错误.我的应用程序虽然没有包含 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 在 Manifest 文件中包含 JAI 的东西,所以它看起来像

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 它.我有一个额外的问题,我包含的一些 jars 覆盖了 META-INF 的服务文件夹中的文件.相反,我使用 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天全站免登陆