Java- export to jar- i/o problems

倾然丶 夕夏残阳落幕 提交于 2019-12-01 14:04:40
Charlie MAI

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

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

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

If you export using "Runnable JAR file" then Eclipse will add a custom ClassLoader and a custom main class into the jar file.

On the same time it seems you have installed some Image-IO extensions into the JDK - something providing the class CLibJPEGImageReaderSpi. On my system (Ubuntu, JDK 1.7) there is no such class but JPEGImageReaderSpi. The CLib part makes me think, that you have installed a native library doing the JPEG reading.

These two parts together seem to make the trouble. Solution - Try to export as a simple jar, start by hand providing the classpath on the commandline. If that works, provide a shell wrapper providing the classpath for easier use.

EDIT Googling around I have found an article with exactly that problem:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!