2013-05-11 43 views
0

我使用Eclipse(Juno和JDK7),并且程序运行在Eclipse上。 我的问题的范围有:Java-导出到jar-i/o问题

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; 

请问有人知道这个问题?

在此先感谢!

+1

'http:// www.MY_IMAGE.jpg' - > 404。为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2013-05-11 09:16:57

+0

M8,这只是一个示例网址;),你可以使用任何你想要的图像(JPG,PNG)。 – gran33 2013-05-11 09:19:15

+0

为什么生气?拍下这张图片:http://www.idautomation.com/ocr-a-and-ocr-b-fonts/new_sizes_ocr.png – gran33 2013-05-11 09:22:22

回答

1

如果使用“Runnable JAR文件”导出,则Eclipse会将自定义ClassLoader和自定义main类添加到jar文件中。

同时,您似乎已经在JDK中安装了一些Image-IO扩展 - 提供了类CLibJPEGImageReaderSpi。在我的系统上(Ubuntu,JDK 1.7)没有这样的类,但JPEGImageReaderSpiCLib部分让我想到,您已经安装了一个执行JPEG读取的本机库。

这两个部分在一起看起来很麻烦。解决方案 - 尝试导出为简单的jar,手动启动,在命令行中提供classpath。如果可行,请提供一个提供类路径的shell包装以便于使用。

编辑周围的Googling我找到了一篇文章用正是问题:

https://www.java.net//node/695773

+0

谢谢,这真的是问题所在,当我生成一个简单的jar时,它的工作原理如下: “在手边提供命令行上的类路径” – gran33 2013-05-11 12:38:07

+0

我的意思是在命令行上'java -classpath SOMETHING_SUITABLE my.qualified.classname.Here' – 2013-05-11 12:52:33

1

我想我可以提供另一种解决方案对于这个问题,因为我前几天终于拿到了这个错误解决这个问题。

  1. 您可以先检查一下这篇文章,在这里解释了原因:Exception when trying to save images ==>综上所述,所需的META-INF的jar需要使用丢失,所以它不能找到“厂商-Name“在MANIFEST.MF中。

  2. 因此,我使用MAVEN生成所需的runnable 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,以便您可以解决此问题。

就是这样。希望这些信息可以帮助你。 =)