Skip to content
Advertisement

Unable to load a groovy classes

I am trying to serialize and deserialize a groovy object with the below class in the jenkins pipeline.

SerializationUtil.groovy

JavaScript

Test.groovy

JavaScript

Jenkins pipeline script

JavaScript

I was able to serialize the object but not deserialize. I am getting the below exception.

JavaScript

From the exception, I could see that GroovyClassLoader is not called and I assume that might be the issue.

Advertisement

Answer

The problem is, as you have identified already, that no GroovyClassLoader gets involved… specifically not the one knowing your current classes.

While an ObjectOutputStream does not really care what classloader a class was defined with the ObjectInputStream has to make assumptions here, since it needs to create an instance. Judging from the trace the “nearest” ClassLoader that woulde be selected for the object instance creation would be the class loader containing the groovy runtime. Sadly that is a common problem with Groovy, as the keep introducing more and more caller sensitive logic in Java.

Anyway, if you also have something like this (ScriptLoaderObjectInputStream.groovy):

JavaScript

and replace your usage of ObjectInputStream with this one, it should work. It should work, because this is a script file as well and the resulting class should have the same class loader your other generated classes get.

For further reading I find this one quite nice: https://rsankarx.wordpress.com/2012/06/08/java-serialization-classloaders/

DISCLAIMER: I wrote this here, no IDE, no spell checking and no testing got involved.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement