Storing a Kryo object in a compiled jar
This really threw me off for a while (cf https://stackoverflow.com/questions/23748254/storing-a-kryo-object-in-a-compiled-jar ).
I had a HashMap
I needed to persist and have fast access to. I used Kryo to serialize the object
I successfully deserialized with
the log.info
showed 231,045 entries in my map.
Now, in order to access the .kryo file after compiling a *-jar-with-dependencies.jar (using Maven) I needed to replace FileInputStream
with MyClass.class.getResourceAsStream
the log.error
never showed and the log.info
said I had 0 entries in my map. Why? isr
was never null so it read something, Kryo just can’t seem to deserialize it and never provided any error.
Well,
Turns out the problem was with Maven. I had “filtering” enabled so Maven was trying to utf8 encode my serialized objects in the jar. Kryo was silent about it but rewriting the code to use standard Java serialization gave the error found here: http://stackoverflow.com/a/5421992/424631. Kryo should probably have warned rather than failed silently (and I should probably make a JIRA…).
Here is the fix: