简体   繁体   中英

Unable to open nested jar file (spring-boot-starter-web) when running from jar

I have a Spring boot application that runs fine via IntelliJ's Spring Boot run configuration as well as with mvn spring-boot:run , however, when I attempt to package via mvn package I get the following error when running from java -jar application.jar

java.io.IOException: Unable to open nested jar file 'lib/spring-boot-starter-web-1.3.3.RELEASE.jar' at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:349) at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:112) at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:96) at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:75) at org.springframework.boot.loader.Launcher.launch(Launcher.java:60) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)

Caused by: java.io.IOException: Unable to find ZIP central directory records after reading 2227 bytes at org.springframework.boot.loader.jar.CentralDirectoryEndRecord.(CentralDirectoryEndRecord.java:64) at org.springframework.boot.loader.jar.JarFile.(JarFile.java:118) at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:387) at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:357) at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:343) ... 5 more

This seems similar to java.io.IOException when running Spring Boot jar on Centos 6.4 / Open JDK 1.7.0 but the solution was to remove a corrupt jar file related to TLD libraries and that question doesn't reference which nested jar couldn't be loaded (so I'm unsure if it's exactly the same issue). I have tried deleting and recreating the ~/.m2/repository directory to no avail. I've also tried different versions of Spring Boot (as I've never run across this issue before) with no luck.

My pom file is below.

<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>com.oranization</groupId>
    <artifactId>application</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--Spring-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <properties>
        <slf4j.version>1.7.12</slf4j.version>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.0.8.RELEASE</version>
        </dependency>
        <!--Spring-->

        <!--Logging-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <!--Logging-->

        <!--Database-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time-hibernate</artifactId>
            <version>1.4</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.jadira.usertype</groupId>
            <artifactId>usertype.core</artifactId>
            <version>3.2.0.GA</version>
        </dependency>
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>

        <!--Database-->

        <!--Joda Time-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.8.1</version>
        </dependency>
        <!--Joda Time-->

        <!--Test-->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.0.14-beta</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.3.2</version>
            <scope>test</scope>
        </dependency>

        <!--Test-->
        <!--Web Support-->
        <dependency>
            <groupId>org.tuckey</groupId>
            <artifactId>urlrewritefilter</artifactId>
            <version>4.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-joda</artifactId>
            <version>2.4.6</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.6.0</version>
        </dependency>
        <!--Web Support-->

        <!--Utils-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.maps</groupId>
            <artifactId>google-maps-services</artifactId>
            <version>0.1.7</version>
        </dependency>
        <dependency>
            <groupId>de.danielbechler</groupId>
            <artifactId>java-object-diff</artifactId>
            <version>0.93.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-csv</artifactId>
            <version>1.2</version>
        </dependency>
        <!--Utils-->

        <!--Reactor-->
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-bus</artifactId>
            <version>2.0.7.RELEASE</version>
        </dependency>
        <!--Reactor-->

        <!--Swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.3.0</version>
        </dependency>
        <!--Swagger-->

        <!--Elasticsearch-->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.1.1</version>
        </dependency>

        <!-- AWS -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.9.33</version>
        </dependency>

        <!--PDF processing-->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>1.8.11</version>
        </dependency>
        <!--PDF processing-->
        <!--Doc/Docx file processing-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.13</version>
        </dependency>
        <!--Doc/Docx file processing-->

        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>application</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

            <!-- Copy Dockerfile -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/docker</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <id>zip-files</id>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <zip destfile="${basedir}/target/app.zip"
                                     basedir="${basedir}/target"
                                     includes="Dockerfile, Dockerrun.aws.json, application.jar"/>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Edit After deleting the ~/m2/repository directory in Finder and emptying the Trash, I did a Reimport from Intellij and then a package from the Maven Projects menu. When trying to run the final product with java -jar target/application.jar I get a new error:

Exception in thread "main" java.lang.IllegalStateException: java.io.IOException: Unable to read bytes at org.springframework.boot.loader.ExecutableArchiveLauncher.(ExecutableArchiveLauncher.java:53) at org.springframework.boot.loader.ExecutableArchiveLauncher.(ExecutableArchiveLauncher.java:45) at org.springframework.boot.loader.JarLauncher.(JarLauncher.java:34) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52) Caused by: java.io.IOException: Unable to read bytes at org.springframework.boot.loader.jar.Bytes.get(Bytes.java:53) at org.springframework.boot.loader.jar.JarEntryData.(JarEntryData.java:70) at org.springframework.boot.loader.jar.JarEntryData.fromInputStream(JarEntryData.java:212) at org.springframework.boot.loader.jar.JarFile.loadJarEntries(JarFile.java:155) at org.springframework.boot.loader.jar.JarFile.(JarFile.java:122) at org.springframework.boot.loader.jar.JarFile.(JarFile.java:104) at org.springframework.boot.loader.jar.JarFile.(JarFile.java:95) at org.springframework.boot.loader.archive.JarFileArchive.(JarFileArchive.java:65) at org.springframework.boot.loader.archive.JarFileArchive.(JarFileArchive.java:61) at org.springframework.boot.loader.Launcher.createArchive(Launcher.java:152) at org.springframework.boot.loader.ExecutableArchiveLauncher.(ExecutableArchiveLauncher.java:50) ... 3 more

Turns out this is related to this question . Despite Java 8 supposedly supporting a huge number of files, I had about 77k in my jar file (due to the inclusion of the node_modules directory from my React frontend) and when I configured the exclude filters properly, it worked like a charm.

Moral of the story, hitting that maximum file limit will cause weird things to happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM