KafkaUtils class not found in Spark streaming
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
KafkaUtils not found in a Spark Streaming job almost always means the Kafka connector jar is missing or mismatched. The fix is usually not in your Scala code at all. It is in dependency coordinates, Scala binary version alignment, or how the job is launched on the cluster.
Add the Correct Kafka Connector
Spark Streaming does not ship every Kafka integration class in the core Spark jar. You need the separate connector module that matches your Spark and Scala versions.
For an sbt build:
For Maven:
The important detail is the suffix such as _2.12. It must match the Scala version used by your Spark distribution. If your cluster runs Spark built for Scala 2.12, depending on a _2.13 artifact will compile badly or fail at runtime.
Import the Right Package and Ship the Jar
Once the dependency is correct, the code should import the kafka010 package:
If this compiles locally but fails when submitted, the cluster probably does not have the connector jar. In that case, include it at submit time:
That solves many "works on my machine" cases because executors also receive the dependency.
Distinguish Old and New Kafka Integrations
A lot of confusing search results come from mixing Spark's older Kafka integration with the newer 0-10 connector. If you copy imports from an old tutorial, you may end up looking for classes in the wrong package or using an artifact that no longer matches your Spark version.
As a rule:
- use
org.apache.spark.streaming.kafka010.KafkaUtilsfor the direct stream connector - keep Spark, Scala, and connector versions aligned
- make sure the runtime classpath on the cluster matches your compile classpath
If you are starting a new project, also consider whether DStreams are the right tool. Spark Structured Streaming is the newer API for many Kafka workloads.
Common Pitfalls
The most common issue is a Scala binary mismatch. A dependency ending in _2.13 will not work with a Spark installation built for _2.12, even if the Spark version number looks correct.
Another frequent mistake is relying on provided dependencies locally and forgetting that the Kafka connector is not actually present on the cluster. Core Spark jars may exist there, but the Kafka integration module often does not.
People also run into problems by importing org.apache.spark.streaming.kafka.KafkaUtils from older examples. Modern Spark Streaming Kafka integration uses the kafka010 package instead.
Finally, if the error appears only at runtime, inspect the full stack trace. A true ClassNotFoundException points to missing jars, while a NoSuchMethodError often means incompatible versions are both present.
Summary
- '
KafkaUtilserrors usually come from missing or mismatched Spark Kafka connector jars.' - The connector artifact must match both the Spark version and the Scala binary version.
- Use imports from
org.apache.spark.streaming.kafka010. - If the job fails only after submission, pass the connector through
--packagesor ship the jar explicitly. - For new systems, evaluate Spark Structured Streaming before committing to DStreams.
Related reading
- Kubernetes executor do not parallelize sub DAGs execution in Airflow
- Lambda Architecture with Apache Spark
- Large data, workflows using pandas
- Large data workflows using pandas
- kafka.zookeeper.ZooKeeperClientTimeoutException Timed out waiting for connection ONLY DURING LISTING TOPICS
- Keep getting permissions error gcloud.container.clusters.get-credentials
- Large scale Machine Learning
- Lazy Method for Reading Big File in Python?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.