繁体   English   中英

java.util.zip.ZipException:使用 SentenceModel 设置的代码长度无效

[英]java.util.zip.ZipException: invalid code lengths set with SentenceModel

我正在使用 OpenNLP 进行句法分析。 我写了这些行来加载句子检测器模型:

    InputStream is = getClass().getClassLoader().getResourceAsStream("models/en-sent.bin") ;            
    SentenceModel sModel = new SentenceModel(is);

上面的行抛出异常: java.util.zip.ZipException : invalid code lengths set。

"models/en-sent.bin"位于"src/main/resources/models/en- token.bin"

当我尝试使用以下方法打印文件路径时:

   System.out.println(getClass().getClassLoader().getResource("models/en-sent.bin"));           

路径显示为: "file:/C:/Users/aaa/workspace/qa/target/classes/models/en-sent.bin" 如果我使用以下方法加载模型:

    InputStream is =  new FileInputStream("src/main/resources/models/en-sent.bin") ;
    SentenceModel sModel = new SentenceModel(is);

它工作正常,但是当我将文件构建到可运行的 jar 中时,此方法将不起作用。 任何帮助为什么抛出这个异常?

完整的堆栈异常在这里:

java.util.zip.ZipException: invalid code lengths set
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.util.zip.ZipInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at opennlp.tools.util.model.PropertiesSerializer.create(PropertiesSerializer.java:31)
at opennlp.tools.util.model.PropertiesSerializer.create(PropertiesSerializer.java:27)
at opennlp.tools.util.model.BaseModel.loadModel(BaseModel.java:224)
at opennlp.tools.util.model.BaseModel.<init>(BaseModel.java:173)
at opennlp.tools.sentdetect.SentenceModel.<init>(SentenceModel.java:91)
at com.elsevier.sherpath.syntactic_analysis.QuestionNLPAnalysis.<init>(QuestionNLPAnalysis.java:88)
at com.elsevier.sherpath.main.QuestionProcessing.syntacticAnalysis(QuestionProcessing.java:255)
at com.elsevier.sherpath.main.BatchProcess.main(BatchProcess.java:23)

谢谢

您可以将resource filtering设置为 false 并包含该 bin 文件,这样它就不会在mvn install受到影响。

<resource>
    <directory>your resources directory</directory>
    <filtering>false</filtering>
        <includes>
            <include>your-bin.bin</include>
        </includes>
</resource>

异常的原因是 maven 正在尝试使用pom.xml属性重新创建您的 bin 文件,因此,目标目录中的bin文件将与您的资源文件夹中的不同。 因此,将bin文件的resource filtering设置为 false。

它工作正常,但是当我将文件构建到可运行的 jar 中时,此方法将不起作用。 任何帮助为什么抛出这个异常?

   InputStream is =  new FileInputStream("src/main/resources/models/en-sent.bin") ;

使用依赖于系统的文件名。
当您不在 JAR 中时,它会将您项目的文件夹用作基本路径。
当您在 JAR 中时,它使用 JAR 本身作为基本路径。
所以它不能工作。

您有两种方法可以解决您的问题:

  • 您可以通过指定类路径中的资源来使用类路径资源加载。 例如InputStream is = YourClass.class.getResourceAsStream("/models/en-sent.bin");
    在您的尝试中,我认为您忘记了路径开头的"/"
    如果没有"/"您将使用相对于调用方法的包的路径。

  • 或者您将资源移到 JAR 之外并使用绝对路径访问它。 例如: InputStream is = new FileInputStream("file:///D:/models/en-sent.bin"); .

我们也可以使用这个插件

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <nonFilteredFileExtensions>
                    <nonFilteredFileExtension>bin</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
            </configuration>
        </plugin>

暂无
暂无

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

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