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

查看:46
本文介绍了Java-导出到jar-i/o问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Eclipse(带有 JDK7 的 Juno)上工作,并且程序(在 Eclipse 上)运行良好.我有问题的线路是:

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);

但是当我将项目导出到 jar 文件并尝试通过 Windows(7-64 位)命令行运行它时,出现以下错误:

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

我也使用那个导入:

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?

提前致谢!

推荐答案

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

I guess I can provide another solution as to this question since I got this error a few days ago and finally solve it.

  1. 你可以先看看这篇文章,这里解释了原因:Exception when尝试保存图像==> 综上所述,jar 需要用到的 META-INF 缺失,所以在 MANIFEST.MF 中找不到vender-Name".

  1. 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.

因此,我使用 MAVEN 生成所需的可运行 jar,而不是使用 Eclipse 生成它.如何?可以写一个pom.xml来实现,记得在jar文件中使用maven-assembly-plugin"生成需要的MANIFEST.MF.这是关键步骤.我也可以给你一个样本(pom.xml):

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>

这意味着 maven 会在 jar 中为您添加所需的 META-INFO,以便您可以解决此问题.

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. =)

这篇关于Java-导出到jar-i/o问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆