0

I'm trying to run my kafka project on docker. If I run this project on my local it works correctly but If I run on docker I got this error message

   Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/kafka/clients/producer/Producer
        at com.kafka.App.main(App.java:14)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.producer.Producer
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

I'm not sure but it looks like can't load dependencies from pom.xml.

Here is my Dockerfile

FROM maven:3.5-jdk-8 as maven
WORKDIR /app
COPY ./pom.xml ./pom.xml
RUN mvn dependency:go-offline -B
COPY ./src ./src
RUN mvn package && cp target/kafka-producer-*.jar app.jar
RUN apk add --no-cache bash
WORKDIR /app
COPY --from=maven /app/app.jar ./app.jar
CMD ["java", "-jar", "/app/app.jar"]

And last thing App:14 creating new instance from SimpleProducer class and the error line is

import com.kafka.Interfaces.IKafkaConstants;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;      ***HERE***
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.LongSerializer;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;

public class SimpleProducer {
4
  • 1
    You should pre-package your application (with all its dependencies) in a single jar and use this jar to build your container... Commented Jun 17, 2019 at 20:15
  • Yeah actually I'm doing as you said with "mvn clean package" and then I'm building it "docker build . -t foobar" @Turing85 Commented Jun 17, 2019 at 21:23
  • That doesn't mean that your JAR contains all the declared dependencies. You need to create an uber jar Commented Jun 18, 2019 at 0:16
  • 1
    It works with uber jar. Thanks @cricket_007 Commented Jun 18, 2019 at 14:03

1 Answer 1

1

Based on the error, it seems like your JAR doesn't contain your declared dependencies.

You should get a similar error doing java -jar "locally", and you can get around the error using the shade plugin, for example to make an uber jar.

Or, I would suggest using jib-maven-plugin to build the container, which I beleive does put all declare dependencies into the container image, and you don't need a Dockerfile. (I have no affiliation)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.