简体   繁体   English

由于“无法打开嵌套条目”而无法运行 Spring-Boot jar 文件

[英]Can't run a Spring-Boot jar file due to "Unable to open nested entry"

I know there are a dozen questions like this out there but I didn't find any of their solutions suitable for my case!我知道有很多这样的问题,但我没有找到适合我的情况的解决方案!

So, I'm trying to pack a jar file for a Spring-Boot project using Maven.所以,我正在尝试使用 Maven 为 Spring-Boot 项目打包一个 jar 文件。 I've created the whole project using IntelliJ IDEA and it runs under IDE.我使用 IntelliJ IDEA 创建了整个项目,它在 IDE 下运行。 In order to build the package, I use mvn package from the command line.为了构建包,我从命令行使用mvn package

The generated.jar file can be run on my development machine too but when I copy the.jar file to some raw docker container, it gives out the following error:生成的 .jar 文件也可以在我的开发机器上运行,但是当我将 .jar 文件复制到某个原始 docker 容器时,它会出现以下错误:

Exception in thread "main" java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/lucene-highlighter-5.4.1.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file

And here's my pom.xml file:这是我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.2.0</version>
        </dependency>
    </dependencies>

</project>

As it turned out, it had nothing to do with Maven and/or Spring-Boot. 事实证明,它与Maven和/或Spring-Boot无关。 In case someone else might face the same issue, here's what I've done that led to this problem: 如果其他人可能面临同样的问题,这就是我所做的导致这个问题:

As a part of my deployment process, I would unzip the jar file, revise the config.properties and zip it again. 作为我的部署过程的一部分,我将解压缩jar文件,修改config.properties并再次压缩它。 It seems for some reason (unknown to myself), the generated .jar file is not executable and leads to the mentioned error. 似乎由于某种原因(我自己不知道),生成的.jar文件不可执行并导致提到的错误。

My workaround was not to uncompress the .jar file, instead just replace the config.properties with a single command, like zip ./project.jar ./config.properties , while the project.jar is already in the current path. 我的解决方法是不解压缩.jar文件,而只是使用单个命令替换config.properties,例如zip ./project.jar ./config.properties ,而project.jar已经在当前路径中。 This one works! 这一个有效!

I also faced this issue and tried the following way which worked well for me. 我也遇到了这个问题并尝试了以下对我有用的方法。

NOTE : Dont' extract and build new jar again. 注意: 不要'再次提取和构建新罐。 Simply extract and update existing jar with modified file. 只需使用修改后的文件提取和更新现有jar。

For example, there is a jar namely myjar.jar in lab_directory 例如,lab_directory中有一个jar,即myjar.jar

i) move to lab_directory and extract myjar.jar ( which has dir1/config.properties file ) i)移动到lab_directory并解压缩myjar.jar(其中包含dir1 / config.properties文件)
ii) copy dir1 in extracted folder to lab_directory folder ii)将解压缩文件夹中的dir1复制到lab_directory文件夹
iii) modify lab_directory/dir1/config.properties iii)修改lab_directory / dir1 / config.properties
iv) Run following command in lab_directory iv)在lab_directory中运行以下命令
jar -uf myjar.jar dir1/config.properties jar -uf myjar.jar dir1 / config.properties

That's it. 而已。 Existing myjar.jar has been updated with modified config.properties file now. 现在,myjar.jar已使用修改后的config.properties文件进行更新。

It has been compressed and nested jar files must be stored without compression.它已被压缩,嵌套的 jar 文件必须在不压缩的情况下存储。 Please check the mechanism used to create your executable jar file请检查用于创建可执行 jar 文件的机制

It just means that we should use no compression by a command like this:这只是意味着我们应该通过这样的命令不使用压缩

jar -uvf0 example_app.war DIR_TO_ADD jar -uvf0 example_app.war DIR_TO_ADD

Where "0" means that no compression will be used.其中“0”表示不使用压缩。

See "0" options in: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/jar.html#options请参阅“0”选项: https ://docs.oracle.com/javase/7/docs/technotes/tools/windows/jar.html#options

Such a way helped me to add the directory with files and run the java without errors like in the subject.这种方式帮助我添加了包含文件的目录并运行 java 而没有像主题中那样的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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