NoClassDefFoundError – missing dependencies in the classpath



My problem

I have written some java-code. The code runs flawlessly in intellij.
But when I run it as a .jar file (i.e., the command java -jar app.jar), I get the error message:
Unable to initialize main class RSocketClient.Client
Caused by: java.lang.NoClassDefFoundError: io/rsocket/transport/ClientTransport

My Research

I have been searching about the NoClassDefFoundError on google and stackoverflow and have found that the error arises when dependencies are missing. The solution seems to be that I need to add dependencies to the classpath or maven repository. But I don’t know how to do this (my pom.xml is presented below).

My Question

All I want to do is to turn my program into a .jar file and run it through my terminal (Windows10 OS). I appreciate all the help that I can get.

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>org.example</groupId>
    <artifactId>RSocketSample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>RSocketClient.Client</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>io.rsocket</groupId>
            <artifactId>rsocket-core</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>io.rsocket</groupId>
            <artifactId>rsocket-transport-netty</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.32</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.32</version>
        </dependency>
    </dependencies>

</project>

Answer

It looks like your jar doesn’t contain classes of RSocket itself, and its fine. Jar created by maven contains only your classes (in your module).

The jar of rsocket (as well as other jars that you’re using,like SLF4J stuff for example) reside in the corresponding jars prepared by the maintainers of these third-parties.

IntelliJ “sees” the whole classpath (that includes all the dependencies) and runs your application with all this classpath.

Probably what you should do is create a jar including dependencies: See This SO thread

Another option is keeping your jar “as is” but requiring that it will be run with all the dependencies in the classpath, but it looks like its not what you’re trying to do, this way is used when you’re developing a library and not a standalone application.



Source: stackoverflow