我在Eclipse(使用JDK7的Juno)上工作,程序运行(在Eclipse上)很好。 我的问题是:
URL imageURL = new URL("http://www.idautomation.com/ocr-a-and-ocr-b-fonts/new_sizes_ocr.png");
RenderedImage img = ImageIO.read(imageURL);
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);
但是当我将项目导出到jar文件并尝试通过windows(7-64位)命令行运行时,会出现以下错误:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.util.ServiceConfigurationError: javax.imageio.spi.ImageReaderSpi: Providercom.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReaderSpi could not be instantiated: java.lang.IllegalArgumentException: vendorName == null!
at java.util.ServiceLoader.fail(Unknown Source)
at java.util.ServiceLoader.access$100(Unknown Source)
at java.util.ServiceLoader$LazyIterator.next(Unknown Source)
at java.util.ServiceLoader$1.next(Unknown Source)
at javax.imageio.spi.IIORegistry.registerApplicationClasspathSpis(Unknow
n Source)
at javax.imageio.spi.IIORegistry.<init>(Unknown Source)
at javax.imageio.spi.IIORegistry.getDefaultInstance(Unknown Source)
at javax.imageio.ImageIO.<clinit>(Unknown Source)
at SimpleQueueServiceSample.testOCR(SimpleQueueServiceSample.java:75)
at SimpleQueueServiceSample.main(SimpleQueueServiceSample.java:69)
... 5 more
Caused by: java.lang.IllegalArgumentException: vendorName == null!
at javax.imageio.spi.IIOServiceProvider.<init>(Unknown Source)
at javax.imageio.spi.ImageReaderWriterSpi.<init>(Unknown Source)
at javax.imageio.spi.ImageReaderSpi.<init>(Unknown Source)
at com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReaderSpi.<init>(CLibJPEGImageReaderSpi.java:80)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
... 13 more
我也使用那些导入:
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
请有人知道这个问题吗?
提前致谢!
答案 0 :(得分:2)
我想我可以提供另一个解决这个问题的方法,因为我几天前收到了这个错误并最终解决了。
您可以先查看这篇文章,在此解释原因:Exception when trying to save images ==&GT;总而言之,jar需要使用的META-INF缺失,因此无法在MANIFEST.MF中找到“vender-Name”。
因此,我使用MAVEN生成所需的可运行jar,而不是使用Eclipse生成它。怎么样?您可以编写一个pom.xml来实现它,并记得使用“maven-assembly-plugin”在jar文件中生成所需的MANIFEST.MF。这是关键的一步。我也可以给你一个样本(pom.xml):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xxxProject</groupId>
<artifactId>xxxProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>oss.sonatype.org</id>
<name>Sonatype Snapshot Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.demo.Main</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
<Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
</dependencies>
所以,最重要的部分是:
<manifestEntries>
<Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
<Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
</manifestEntries>
这意味着maven将在jar中为您添加所需的META-INFO,以便您可以解决此问题。
就是这样。希望这些信息可以帮到你。 =)
答案 1 :(得分:1)
如果使用“Runnable JAR file”导出,则Eclipse会将自定义ClassLoader
和自定义main
类添加到jar文件中。
同时,您似乎已经在JDK中安装了一些Image-IO扩展 - 提供类CLibJPEGImageReaderSpi
的东西。在我的系统(Ubuntu,JDK 1.7)上没有这样的类,只有JPEGImageReaderSpi
。 CLib
部分让我想到,您已经安装了一个执行JPEG读取的本机库。
这两个部分似乎在一起造成麻烦。解决方案 - 尝试导出为一个简单的jar,手动启动在命令行上提供类路径。如果可行,请提供一个shell包装器,提供类路径以便于使用。
编辑谷歌搜索我发现了一篇文章正是这个问题: