【问题标题】:Java- export to jar- i/o problemsJava- 导出到 jar- i/o 问题
【发布时间】:2013-05-11 09:11:15
【问题描述】:

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

请问有人知道这个问题吗?

提前致谢!

【问题讨论】:

标签: java executable-jar java-io javax.imageio


【解决方案1】:

如果您使用“可运行的 JAR 文件”导出,则 Eclipse 将在 jar 文件中添加自定义 ClassLoader 和自定义 main 类。

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

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

编辑谷歌搜索我发现一篇文章正是有这个问题:

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

【讨论】:

  • 谢谢,确实是问题所在,当我生成一个简单的 jar 时它可以工作,但是如何:“在命令行上手动提供类路径”
  • 我的意思是在命令行上类似于java -classpath SOMETHING_SUITABLE my.qualified.classname.Here
【解决方案2】:

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

  1. 你可以先看看这篇文章,这里解释一下原因:Exception when trying to save images ==> 综上所述,jar 需要使用的必需 META-INF 缺失,所以在 MANIFEST.MF 中找不到“vender-Name”。

  2. 因此,我使用 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,以便您解决此问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    相关资源
    最近更新 更多