I have changed the package of a class in a shared library. I have Consumer A and B consuming this library. Consumer A uses Consumer B as well.
I’m working on Consumer A right now and I’ve updated the .jar file of the shared library. Now at runtime I’m getting a java.lang.NoClassDefFoundError for the old package, presumably because Consumer B still uses it (this is using Spring DI, has a ton of dependencies and I’m just starting to work in Java, so I’m not 100% sure yet where else it could come from).
In .net, I could use Type Forwarding to temporarily remedy the situation until I can also update Consumer B. Does a similar mechanism also exist in Java? (“when someone asks for de.stum.old.MyObject, send them to de.stum.new.MyObject instead”)
No, the JVM has no type forwarding feature. You’ll have to take another approach:
You could define the new class as subclass of the old (or the other way around), and leave both classes until all consumers have updated. Of course, the only allows passing of MyObjects among consumers in one direction.
If you don’t need assignment compatibility, you could load both the old and new jar at the same time (assuming no name conflicts, for instance because all classes have been moved to a new package. If you do have name conflicts, you might wish to resolve them using a separate class loader).
Of course, if you want to get real fancy you could replace references to moved classes when you load the referring classes by using a bytecode manipulation library.