Search code examples
javaelasticsearchjacksonfasterxml

Getting issue with elastic search java client - Caused by: java.lang.NoClassDefFoundError: co/elastic/clients/json/JsonpMapper


pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>es</groupId>
  <artifactId>es-test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>es-test</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>7.17.9</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>
      <dependency>
      <groupId>jakarta.json</groupId>
      <artifactId>jakarta.json-api</artifactId>
      <version>2.0.1</version>
    </dependency>
  </dependencies>
</project>

src/main/java/es/App.java

package es;


import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch.core.IndexRequest;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;

class Doc {
    String name = "Rahul Kumar";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public class App {
    public static void main(String[] args) throws ElasticsearchException, IOException {
        String cloudId = "...";
        String apiKey = "...";

        RestClient restClient = RestClient.builder(cloudId)
                .setDefaultHeaders(new Header[] { new BasicHeader("Authorization", "ApiKey " + apiKey) }).build();

        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        // And create the API client
        ElasticsearchClient client = new ElasticsearchClient(transport);

        ElasticsearchAsyncClient esAsyncClient = new ElasticsearchAsyncClient(transport);

        IndexRequest.Builder<Doc> indexReqBuilder = new IndexRequest.Builder<Doc>();
        indexReqBuilder.index("test-service");
        indexReqBuilder.id("12");
        indexReqBuilder.document(new Doc());

        IndexResponse response1 = client.index(indexReqBuilder.build());
        System.out.println("Success: " + response1.version());

    }

}

Upon running mvn package and java -cp es-test-1.0-SNAPSHOT.jar es.App I am getting the following error.

Error: Unable to initialize main class es.App
Caused by: java.lang.NoClassDefFoundError: co/elastic/clients/json/JsonpMapper

This issue is only coming on ubuntu running on EC2, while there is no issue if i run it locally over my mac machine inside eclipse.

Maven Version

Apache Maven 3.9.0 (9b58d2bad23a66be161c4664ef21ce219c2c8584)
Maven home: /home/apadmin/mvn-install/apache-maven-3.9.0
Java version: 17.0.5, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-1033-azure", arch: "amd64", family: "unix"

JDK Version

openjdk 17.0.5 2022-10-18
OpenJDK Runtime Environment (build 17.0.5+8-Ubuntu-2ubuntu120.04)
OpenJDK 64-Bit Server VM (build 17.0.5+8-Ubuntu-2ubuntu120.04, mixed mode, sharing)

Edit - If I run the project inside using right-click + run as JAVA application then it's working fine. But if i run the same project using mvn package && java -cp target/es-test-0.0.1-SNAPSHOT.jar es.App then i am facing the same issue even on mac


Solution

  • You are building the app, but not including any of its dependencies. Maven doesn't do that automagically. To include dependencies, use something like maven-assembly-plugin. Put this right after the dependencies section of your pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        es.App
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>