Home > Software engineering >  NoClassDefFoundError: StringSerializer
NoClassDefFoundError: StringSerializer

Time:12-19

I compiled a project with dependency:

    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>1.1.0</version>
    </dependency>

the jar file contains:

C:\Users\abc\IdeaProjects\KafkaProducer\target>"C:\Program Files\Java\jdk1.8.0_201\bin\jar.exe" tvf KafkaProducer-1.0-SNAPSHOT.jar
   250 Sun Dec 18 20:43:50 EST 2022 META-INF/MANIFEST.MF
     0 Sun Dec 18 20:43:50 EST 2022 META-INF/
     0 Sun Dec 18 17:46:14 EST 2022 kafka/
     0 Sun Dec 18 17:46:14 EST 2022 kafka/Demo/
     0 Sun Dec 18 20:43:50 EST 2022 META-INF/maven/
     0 Sun Dec 18 20:43:50 EST 2022 META-INF/maven/kafka.Demo/
     0 Sun Dec 18 20:43:50 EST 2022 META-INF/maven/kafka.Demo/KafkaProducer/
  1752 Sun Dec 18 20:43:50 EST 2022 kafka/Demo/ProducerDemo.class
  1585 Sun Dec 18 20:24:46 EST 2022 META-INF/maven/kafka.Demo/KafkaProducer/pom.xml
   120 Sun Dec 18 18:06:56 EST 2022 META-INF/maven/kafka.Demo/KafkaProducer/pom.properties

the class path is correctly set in MANIFEST.MF:

Manifest-Version: 1.0
Built-By: abc
Class-Path: lib/kafka-clients-1.1.0.jar lib/lz4-java-1.4.jar lib/snapp
 y-java-1.1.7.1.jar lib/slf4j-api-1.7.25.jar
Created-By: Apache Maven 3.8.1
Build-Jdk: 1.8.0_201
Main-Class: kafka.Demo.ProducerDemo

When I ran the jar file, I got the following error:

C:\Users\abc\IdeaProjects\KafkaProducer\target>java -jar .\KafkaProducer-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/kafka/common/serialization/StringSerializer
        at kafka.Demo.ProducerDemo.main(ProducerDemo.java:18)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.common.serialization.StringSerializer
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

the class can be found in kafka-clients-1.1.0.jar:

C:\Users\abc\IdeaProjects\KafkaProducer\target>"C:\Program Files\Java\jdk1.8.0_201\bin\jar.exe" tvf kafka-clients-1.1.0.jar|findstr StringSerializer
  2126 Fri Mar 23 23:02:08 EDT 2018 org/apache/kafka/common/serialization/StringSerializer.class

if I run it a different way by specifying all jars and the main class, then it complains it can not load main class:

C:\Users\abc\IdeaProjects\KafkaProducer\target>java -cp .\kafka-clients-1.1.0.jar:.\KafkaProducer-1.0-SNAPSHOT.jar kafka.Demo.ProducerDemo
Error: Could not find or load main class kafka.Demo.ProducerDemo

Where did I get wrong?

CodePudding user response:

Create an uber, executable JAR from Maven. Then it will have all the dependencies.

https://maven.apache.org/plugins/maven-assembly-plugin/usage.html#usage

  • Related