elasticsearch-java icon indicating copy to clipboard operation
elasticsearch-java copied to clipboard

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

Open bhavya475 opened this issue 3 years ago • 1 comments
trafficstars

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

bhavya475 avatar Jun 09 '22 09:06 bhavya475

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?

swallez avatar Jun 16 '22 16:06 swallez

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 avatar Jul 18 '23 15:07 ngsoftwaredev

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.

ngsoftwaredev avatar Jul 18 '23 15:07 ngsoftwaredev

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.

PriyanshMaheshwari avatar Jun 03 '24 14:06 PriyanshMaheshwari