![](/img/trans.png)
[英]Caused by: java.util.zip.ZipException: invalid code lengths set
[英]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.