2013-05-11 217 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,以便您可以解決此問題。

就是這樣。希望這些信息可以幫助你。 =)