Kafka Producer (via CoreUtils.createObject) effectively uses Class.forName to load encoder classes. Class.forName is by design finds classes only in the defining classloader of the enclosing class (which is often the bootstrap class loader). It does not use the current thread context class loader. This can lead to problems in environments where classes are dynamically loaded and therefore may not be present in the bootstrap classloader.
This leads to ClassNotFound Exceptions in environments like Spark where classes are loaded dynamically using custom classloaders. Issues like this have reported. E.g. -
Other references regarding this issue with Class.forName
This is a problem we have faced repeatedly in Apache Spark and we solved it by explicitly specifying the class loader to use. See