Java-导出到jar-i/o问题[英] Java- export to jar- i/o problems

问题描述

我在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;

请有人知道问题吗?

预先感谢!

推荐答案

如果您使用"可运行的JAR文件"导出,则Eclipse将在JAR文件中添加自定义ClassLoader和一个自定义main类.

同时,您似乎已经在JDK中安装了一些image -io扩展名 - 提供了类CLibJPEGImageReaderSpi.在我的系统(Ubuntu,JDK 1.7)上,没有这样的类,而是JPEGImageReaderSpi. CLib部分让我想,您已经安装了一个本机库进行JPEG阅读.

这两个部分在一起似乎造成了麻烦.解决方案 - 尝试以简单的jar导出,从手工开始,在命令行上提供类路径.如果有效的话,请提供一个提供班级路径的外壳包装器,以便于使用.

编辑搜索谷歌搜索,我发现了一篇有问题的文章:

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

其他推荐答案

我想我可以为这个问题提供另一个解决方案,因为几天前我遇到了这个错误,最后解决了.

  1. 您可以首先检查本文,此处解释了:异常何时试图保存图像 ==>总结一下,jar需要使用的所需元I-INF缺少,因此在清单中找不到" vender-name".mf.

  2. 因此,我使用maven生成所需的可运行的jar,而不是使用eclipse生成它.如何?您可以编写pom.xml来实现它,并记住使用" maven-iSembly-plugin"来生成JAR文件中所需的subtest.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中为您添加所需的元INFO,以便您可以解决此问题.

就是这样.希望这些信息可以帮助您. =)

本文地址:https://www.itbaoku.cn/post/978467.html