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

huangapple go评论67阅读模式
英文:

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

问题

## pom.xml
```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


<details>
<summary>英文:</summary>
## pom.xml
```xml
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;es&lt;/groupId&gt;
&lt;artifactId&gt;es-test&lt;/artifactId&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;name&gt;es-test&lt;/name&gt;
&lt;url&gt;http://maven.apache.org&lt;/url&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;co.elastic.clients&lt;/groupId&gt;
&lt;artifactId&gt;elasticsearch-java&lt;/artifactId&gt;
&lt;version&gt;7.17.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
&lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
&lt;version&gt;2.12.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;jakarta.json&lt;/groupId&gt;
&lt;artifactId&gt;jakarta.json-api&lt;/artifactId&gt;
&lt;version&gt;2.0.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;

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 = &quot;Rahul Kumar&quot;;

	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 = &quot;...&quot;;
		String apiKey = &quot;...&quot;;

		RestClient restClient = RestClient.builder(cloudId)
				.setDefaultHeaders(new Header[] { new BasicHeader(&quot;Authorization&quot;, &quot;ApiKey &quot; + 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&lt;Doc&gt; indexReqBuilder = new IndexRequest.Builder&lt;Doc&gt;();
		indexReqBuilder.index(&quot;test-service&quot;);
		indexReqBuilder.id(&quot;12&quot;);
		indexReqBuilder.document(new Doc());

		IndexResponse response1 = client.index(indexReqBuilder.build());
		System.out.println(&quot;Success: &quot; + 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: &quot;linux&quot;, version: &quot;5.15.0-1033-azure&quot;, arch: &quot;amd64&quot;, family: &quot;unix&quot;

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 &amp;&amp; java -cp target/es-test-0.0.1-SNAPSHOT.jar es.App then i am facing the same issue even on mac

答案1

得分: 1

这部分内容的中文翻译如下:

你正在构建该应用程序,但没有包括任何依赖项。 Maven 不会自动完成这个任务。要包括依赖项,请使用类似 maven-assembly-plugin 的工具。将以下内容添加到你的 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>

希望这有帮助。如果你有任何其他问题,请随时提出。

英文:

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:

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

huangapple
  • 本文由 发表于 2023年2月14日 01:40:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定