简体   繁体   中英

Maven command line execution error ClassNotFoundException

I am using Kafka and I have created a simple maven project to use Kafka Streams API. When I execute the project from intelliJ it runs properly. When I try to run the project from the command line I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/kafka/common/serialization/Serdes
at pega.main(pega.java:18)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.common.serialization.Serdes
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
... 1 more

the java code and the pom.xml are the following:

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>vodafone.vfgr.pega</groupId>
    <artifactId>s_order_project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-streams -->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-streams</artifactId>
            <version>2.3.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Pega.java

public class pega {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put(StreamsConfig.APPLICATION_ID_CONFIG, "pega-app");
        properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        properties.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        properties.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        properties.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 1000);

        StreamsBuilder builder = new StreamsBuilder();

        KStream<String, String> s_order = builder.stream("gg-input-topic", Consumed.with(Serdes.String(),Serdes.String()))
                .selectKey((k,v) -> v);
        s_order.to("pega-output-topic", Produced.with(Serdes.String(),Serdes.String()));

        KafkaStreams streams = new KafkaStreams(builder.build(), properties);
        streams.start();

        System.out.println(streams.toString());

        Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
    }
}

The commands I execute in terminal are the following:

mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< vodafone.vfgr.pega:s_order_project >-----------------
[INFO] Building s_order_project 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ s_order_project ---
[INFO] Deleting /home/christoforos/IdeaProjects/s_order_project/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.813 s
[INFO] Finished at: 2020-02-28T17:15:11+02:00
[INFO] ------------------------------------------------------------------------
mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< vodafone.vfgr.pega:s_order_project >-----------------
[INFO] Building s_order_project 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ s_order_project ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ s_order_project ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/christoforos/IdeaProjects/s_order_project/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ s_order_project ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/christoforos/IdeaProjects/s_order_project/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.0:testCompile (default-testCompile) @ s_order_project ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ s_order_project ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ s_order_project ---
[INFO] Building jar: /home/christoforos/IdeaProjects/s_order_project/target/s_order_project-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.474 s
[INFO] Finished at: 2020-02-28T17:15:23+02:00
[INFO] ------------------------------------------------------------------------
java -cp target/s_order_project-1.0-SNAPSHOT.jar pega

And there is where I get the error. Could you help me with this issue. (I remind when I execute from intelliJ it runs correctly)

It is not running on terminal because required dependencies are not in classpath. And it is running in IntelliJ because dependencies are available in IntelliJ IDEA's classpath.

You have two options to run:

  1. In order to run it on terminal, you need to specify classpath for all the required jars.
  2. Or simply specify below maven plugin into pom.xml to generate fat jar(jar with dependencies) and run as you are running.
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.1</version>

    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>

    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Hope this helps!

Try using package hierarchy instead of pega.Hope it will help.

java - cp target/s_order_project-1.0-SNAPSHOT.jar com.example.pega

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM