问题
I work on Eclipse (Juno with JDK7), and the program runs (on Eclipse) fine. My problematic lines are:
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);
But when i export the project to a jar file and try to run it via windows (7- 64 bit) command line, the following error appears:
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
I also using that imports:
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
Please, someone know the problem?
Thanks in advance!
回答1:
I guess I can provide another solution as to this question since I got this error a few days ago and finally solve it.
You can check this article first, here explained the reason: Exception when trying to save images ==> To sum up, the required META-INF the jar need to use is missing, so it can't find the "vender-Name" in the MANIFEST.MF.
As a result, I use MAVEN to generate the required runnable jar instead of using Eclipse to generate it. How? You can write a pom.xml to achieve it and remember to use "maven-assembly-plugin" to generate the required MANIFEST.MF in the jar file. This is the key step. And I can also give you a sample(pom.xml) for it:
<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>
So, the most important part is:
<manifestEntries>
<Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
<Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
</manifestEntries>
That means maven will add the required META-INFO for you in the jar so that you can solve this issue.
That's it. Hope these info can help you. =)
回答2:
If you export using "Runnable JAR file" then Eclipse will add a custom ClassLoader
and a custom main
class into the jar file.
On the same time it seems you have installed some Image-IO extensions into the JDK - something providing the class CLibJPEGImageReaderSpi
. On my system (Ubuntu, JDK 1.7) there is no such class but JPEGImageReaderSpi
. The CLib
part makes me think, that you have installed a native library doing the JPEG reading.
These two parts together seem to make the trouble. Solution - Try to export as a simple jar, start by hand providing the classpath on the commandline. If that works, provide a shell wrapper providing the classpath for easier use.
EDIT Googling around I have found an article with exactly that problem:
https://www.java.net//node/695773
来源:https://stackoverflow.com/questions/16495555/java-export-to-jar-i-o-problems