elastic / elasticsearch-java

Official Elasticsearch Java Client
Apache License 2.0
414 stars 241 forks source link

Getting this error java.lang.ClassNotFoundException: jakarta.json.JsonException #311

Closed bhavya475 closed 7 months ago

bhavya475 commented 2 years ago

Java API client version

8.1.3

Java version

1.8

Elasticsearch Version

8.1.3

Problem description

Getting this error java.lang.ClassNotFoundException: jakarta.json.JsonException tags changed: → language-clients Failed to instantiate [co.elastic.clients.Elasticsearch.ElasticsearchClient]: Factory method 'client' threw exception; nested exception is java.lang.NoClassDefFoundError: jakarta/json/JsonException

@bean public ElasticsearchClient client() throws Exception { RestClient restClient = RestClient.builder( new HttpHost(esHost, esPort)).build();

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

// And create the API client return new ElasticsearchClient(transport); } Dependency:

org.springframework.boot spring-boot-starter-data-jpa 2.6.7 mysql mysql-connector-java 8.0.29 org.springframework.boot spring-boot-starter-data-redis 2.6.7 redis.clients jedis 4.2.3 com.paytm ncmc-acquirer-bus-common 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-test 2.6.7 test org.mockito mockito-all 1.10.19 test junit junit 4.13.2 test org.projectlombok lombok 1.18.24 co.elastic.clients elasticsearch-java 8.1.3 com.fasterxml.jackson.core jackson-databind 2.12.3 jakarta.json jakarta.json-api 2.0.1
swallez commented 2 years ago

This sounds similar to ClassNotFoundException: jakarta.json.spi.JsonProvider that other users have encountered.

Can you use the instructions provided at in https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/installation.html#class-not-found-jsonprovider and report if this resolves the issue?

ngsoftwaredev commented 1 year ago

Hi,

I'm having the same issue with version 8.8.2 of the client. Adding the dependency does not solve it.

It is tied to using Spring Boot starter version 2.7 as parent:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.12</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

If I switch to version 3.1.1 I don't reproduce the issue...

And it's a real pain in the neck since I need this 2.7 version due to Azure dependencies that don't handle Spring 3...

@bhavya475 are you usgin Spring 2.7 also per chance?

ngsoftwaredev commented 1 year ago

I actually found a workaround, by chance I stumbled upon this StackOverflow post: https://stackoverflow.com/questions/68040881/jaxb-moxy-unmarshalling-json-runs-into-error-exception-in-thread-main-java-lan

Adding the dependency:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>jakarta.json</artifactId>
   <version>2.0.1</version>
</dependency>

solves the issue, no more ClassNotFoundException on startup.

PriyanshMaheshwari commented 4 months ago

Hi,

I'm facing similar issue Method threw 'java.lang.NoClassDefFoundError' exception and, even after adding the dependencies suggested by @ngsoftwaredev, the issue still persists.

pom.xml spring boot starter version

   <parent>
       <artifactId>spring-boot-starter-parent</artifactId>
       <groupId>org.springframework.boot</groupId>
       <version>2.6.6</version>
       <relativePath/>
   </parent>

pom.xml dependencies specific to ES

       <dependency>
           <groupId>co.elastic.clients</groupId>
           <artifactId>elasticsearch-java</artifactId>
           <version>8.13.4</version>
       </dependency>
       <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-databind</artifactId>
           <version>2.17.0</version>
       </dependency>
       <dependency>
           <groupId>jakarta.json</groupId>
           <artifactId>jakarta.json-api</artifactId>
           <version>2.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.glassfish</groupId>
           <artifactId>jakarta.json</artifactId>
           <version>2.0.1</version>
       </dependency>

Any help is appreciated. Thanks.

DongJianquan commented 12 hours ago

Hi, @PriyanshMaheshwari

The issue you're facing seems to be caused by the fact that spring-boot-dependencies-2.6.6.pom sets <jakarta-json.version>1.1.6</jakarta-json.version>, which overrides the version you specified (2.0.1).

To resolve this, you can override the jakarta-json.version property in your pom.xml to ensure the correct version is used. Add the following section to your pom.xml under the <properties> tag:

<properties>
    <jakarta-json.version>2.0.1</jakarta-json.version>
</properties>

This should force Spring Boot to use the correct version of jakarta.json. Let me know if this helps!

Thanks.