英文:
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
<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
答案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:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论