ExceptionInInitializer and NoClassDefFound could not initialize class org.elasticsearch.client.RestClientBuilder

Hi All,

I have replaced org.elasticsearch.client.RestHighLevelClient to co.elastic.clients.elasticsearch.ElasticsearchClient . But, when mvn clean verify command is executed, the following exception is being thrown :

[ERROR] Failures:
[ERROR]AbstractTestNGSpringContextTests.run:154->testHighLevelClient» ExceptionInInitializer
[ERROR] RepositoryTest.destroy:393 » NoClassDefFound Could not initialize clas s org.elasticsearch.client.RestClientBuilder
[ERROR] RepositoryTest.init:66->createIndex:359 » NoClassDefFound Could not in itialize class org.elasticsearch.client.RestClientBuilder

Below dependencies have been added to the pom.xml:

co.elastic.clients elasticsearch-java 7.17.20 org.elasticsearch.client elasticsearch-rest-client 7.17.20 org.elasticsearch elasticsearch 7.17.20

Below method is throwing the exception

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;

public ElasticsearchClient newEs7HighLevelClient() {
org.elasticsearch.client.RestClient restClient= org.elasticsearch.client .RestClient.builder(new HttpHost("localhost",9200,"http")).build();
RestClientTransport transport= new RestClientTransport(restClient,new Ja cksonJsonpMapper()) ;
ElasticsearchClient esClient= new ElasticsearchClient(transport);
return esClient;
}

Could anyone suggest some solution to resolve this issue.

Hello and welcome.

The elasticsearch-java dependency already includes elasticsearch-rest-client, so there's no need to import it, also no need to import elasticsearch.

Hi Laura,

Thanks for the inputs. Is that the root cause for this issue. I will remove those imports.

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;

public ElasticsearchClient newHighLevelClient() {
org.elasticsearch.client.RestClient restClient= org.elasticsearch.client .RestClient.builder(new HttpHost("localhost",9200,"http")).build();
RestClientTransport transport= new RestClientTransport(restClient,new Ja cksonJsonpMapper()) ;
ElasticsearchClient esClient= new ElasticsearchClient(transport);
return esClient;
}

The above method is throwing exceptioninInitializer.

Now that the import issue has been solved you can refer to the migration guide and to the getting started guide to learn how to use the new client.

We also have a blog post that explains the migration process in details.