java.util.zip.ZipException: invalid code lengths set with SentenceModel
Solution 1
You can set resource filtering
false and include that bin file, so that it will not be affected in mvn install
.
<resource>
<directory>your resources directory</directory>
<filtering>false</filtering>
<includes>
<include>your-bin.bin</include>
</includes>
</resource>
The cause of the exception is maven is trying to recreate your bin file using pom.xml
properties and, as a result of that, the bin
file in the target directory will not be same as in your resource folder. Therefore set resource filtering
false for the bin
file.
Solution 2
It works fine, but this method will not work when I build the file into a runnable jar. Any help why this Exception is thrown?
InputStream is = new FileInputStream("src/main/resources/models/en-sent.bin") ;
uses a system-dependent file name.
When you are not in a JAR, it uses as base path the folder of you project.
When you are in a JAR, it uses as base path the JAR itself.
So it cannot work.
You have two ways to solve your problem :
either you use a classpath resource loading by specifying the resource that is in the classpath. For example
InputStream is = YourClass.class.getResourceAsStream("/models/en-sent.bin");
In your tries, I think that you have forgotten the"/"
at the beginning of the path.
Without the"/"
you use a path relative to the package where the method is invoked.or you move the resource outside the JAR and you access it with an absolute path. For example :
InputStream is = new FileInputStream("file:///D:/models/en-sent.bin");
.
Elshaimaa Ali
Updated on June 15, 2022Comments
-
Elshaimaa Ali about 2 years
I'm using OpenNLP for syntactic analysis. I wrote these lines to load the Sentence detector model:
InputStream is = getClass().getClassLoader().getResourceAsStream("models/en-sent.bin") ; SentenceModel sModel = new SentenceModel(is);
the above line throws the exception:
java.util.zip.ZipException
: invalid code lengths set.The
"models/en-sent.bin"
is located under"src/main/resources/models/en- token.bin"
.When I try to print the file path using:
System.out.println(getClass().getClassLoader().getResource("models/en-sent.bin"));
The path shows as :
"file:/C:/Users/aaa/workspace/qa/target/classes/models/en-sent.bin"
. if I load the model using:InputStream is = new FileInputStream("src/main/resources/models/en-sent.bin") ; SentenceModel sModel = new SentenceModel(is);
It works fine, but this method will not work when I build the file into a runnable jar. Any help why this Exception is thrown?
The full stack exception is here:
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)
Thanks
-
Elshaimaa Ali about 7 yearsThanks for the response. I used InputStream is = loader.getClass().getResourceAsStream("/models/en-sent.bin"); but SentenceModel sModel = new SentenceModel(is);
-
davidxxx about 7 yearsYou are welcome :) Instead of
loader
, try with a class of your application. -
Elshaimaa Ali about 7 yearsthe path using this line: System.out.println(loader.getClass().getResource("/models/en-sent.bin").getPath()); the result is : /C:/Users/alishaim/workspace/question-assessment/target/classes/models/en-sent.bin the path using this line: System.out.println(loader.getClass().getResource("/models/en-sent.bin").toURI().toString(); the result is : file:/C:/Users/alishaim/workspace/question-assessment/target/classes/models/en-sent.bin Does the format of the path is the problem. I can' use absolute paths, that won't work when running this from the Jar
-
Elshaimaa Ali about 7 yearsI tried with ClassName.Class().getResourceAsStream but still gettin the same problem.
-
wcolen about 7 yearsWhat is your class package? The first "/" is important. Without it the path is relative. Use getResourceAsStream. The getResource will not work outside the IDE.
-
Elshaimaa Ali about 7 yearsSo I added the "/" When I use InputStream is = getClass().getResourceAsStream("/models/en-sent.bin") ; This line : sModel = new SentenceModel(is); throws a java.util.zip.ZipException: invalid code lengths set, when I run it from eclipse When I use: InputStream is = new FileInputStream ("absolute path/en-token.bin"); It works fine through the IDE but of course won't work from the jar because of the path.
-
Rajeev Rathor almost 5 yearsI faced this issue and struggled 1 days. Tried many plugin, solution suggested on stackoverflow in POM.xml. But it was waste. Finally I removed all the contents from .m2 directory and tried to build jar from command "mvn clean package" . It perfectly working for me.
-
Jonas_Hess over 3 yearsI also just recompiled the project and the error was gone