ClassCastException for Interface and implementing class or for same class

  • Problem: In a multimodular project, where multiple modules use the same libraries, you run into ClassCastExceptions for the same class or for interface and correctly implementing class.
  • Solution: Do not add required libraries to each module separately. Instead, wrap all libraries into a dedicated module from which you reexport all packages.
  • Additional information: Each module uses it’s own classloader. During runtime, the identity of a class is not only defined by its fully qualified classname but by it’s class loader as well, which is why “identical” classes loaded by different class loaders cause ClassCastExceptions.
    • Share itShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *