ElasticSearchJava客户端出现问题-原因是:java.lang.NoClassDefFoundError:协同/弹性/客户端/json/JsonpMapper

tktrz96b  于 2023-02-14  发布在  Java
关注(0)|答案(1)|浏览(313)

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());

    }

}

在运行mvn packagejava -cp es-test-1.0-SNAPSHOT.jar es.App时,我收到以下错误。

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

这个问题只会出现在运行在EC2上的ubuntu上,而如果我在eclipse内部的mac机器上本地运行它就不会有问题。

Maven版本

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版本

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)

编辑-如果我运行项目内使用右键单击+运行为JAVA应用程序,那么它的工作很好。但如果我运行相同的项目使用mvn package && java -cp target/es-test-0.0.1-SNAPSHOT.jar es.App,那么我面临着同样的问题,即使在mac上

ca1c2owp

ca1c2owp1#

您正在构建应用程序,但不包括其任何依赖项。Maven不会自动执行此操作。要包括依赖项,请使用maven-assembly-plugin之类的命令。将此命令放在pom.xml的dependencies部分之后:

<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>

相关问题