java - 尝试保存图像时出现异常

标签 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)

最佳答案

正如@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 并且没有修改 list 文件以包含 JAI list 文件的内容或在构建中合并 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 插件合并这些文件。

关于java - 尝试保存图像时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345769/

相关文章:

在 JavaFX 应用程序线程以外的线程上进行 ImageIO 调用的 JavaFX 应用程序在 Mac OS X 上挂起

java - 为什么 JasperReports 会尝试加载 org.springframework.core.io.Resource 类?

java - 为什么我的 Eclipse 可执行 Jar 不能通过双击运行?

java - 构建失败: Cannot find an environment variable

java - T6 压缩 Tiff 图像

java - bufferedImage 的 RGB 精度问题

java - 需要程序使用基本数学运算和 6 个随机数自动找到数字 X

java - 读取 JSON 文件错误

java - 如何在 Spring Batch 中组合多个监听器(步骤、读取、处理、写入和跳过)

java - 如何使用 NIO 从 JAR 中读取文件?