# How to establish a connection from java client to elastic search version 8

**URL:** <https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [October 12, 2022, 7:23am UTC](https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408 "2022-10-12T07:23:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chirag\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chirag_gupta/32/111960_2.png) [@Chirag\_Gupta](https://discuss.elastic.co/u/Chirag_Gupta)\
**Post date:** [October 12, 2022, 7:23am UTC](https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408/1 "2022-10-12T07:23:06Z")

</div>

A guide for establishing a connection of java client with elastic-search without using RestHighLevelClient method

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 12, 2022, 7:24am UTC](https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408/2 "2022-10-12T07:24:22Z")

</div>

Welcome!

Is there anything unclear in [Connecting | Elasticsearch Java API Client [8.4] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html)?

---

<div class="post-metadata">

**Author:** ![Chirag\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chirag_gupta/32/111960_2.png) [@Chirag\_Gupta](https://discuss.elastic.co/u/Chirag_Gupta)\
**Post date:** [October 12, 2022, 7:38am UTC](https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408/3 "2022-10-12T07:38:36Z")

</div>

Thanks dadoonet for replying.  
But when i follow the code snippets given there, it is so much confusing for a new bee to get started with. It throws unable to find valid certification path to requested target error.

```auto
public static void main(String[] args) throws CertificateException {
		// TODO Auto-generated method stub
		
		RestHighLevelClient client = new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost", 9200, "https")));
		
		SearchRequest searchRequest = new SearchRequest();
	    searchRequest.indices("tesla_employees");
	    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
	    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
	    searchRequest.source(searchSourceBuilder);
	    Map<String, Object> map=null;
		Path caCertificatePath = Paths.get("/Users/chirag_gupta/Downloads/elasticsearch-8.4.1/config/certs/http_ca.crt");
		CertificateFactory factory =
				CertificateFactory.getInstance("X.509");
		Certificate trustedCa;
	    try {
	        SearchResponse searchResponse = null;
	        searchResponse =client.search(searchRequest, RequestOptions.DEFAULT);
	        if (searchResponse.getHits().getTotalHits().value > 0) {
	            SearchHit[] searchHit = searchResponse.getHits().getHits();
	            for (SearchHit hit : searchHit) {
	                map = hit.getSourceAsMap();
	                  System.out.println("map:"+Arrays.toString(map.entrySet().toArray()));
	                    
	                
	            }
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    }

	}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 12, 2022, 10:15am UTC](https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408/4 "2022-10-12T10:15:54Z")

</div>

You declared a `client` but you are not using it.

Please don't post images of text as they are hard to read, may not display correctly for everyone, and are not searchable.

Instead, paste the text and format it with `</>` icon or pairs of triple backticks (```), and check the preview window to make sure it's properly formatted before posting it. This makes it more likely that your question will receive a useful answer.

It would be great if you could update your post to solve this.

---

<div class="post-metadata">

**Author:** ![Chirag\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chirag_gupta/32/111960_2.png) [@Chirag\_Gupta](https://discuss.elastic.co/u/Chirag_Gupta)\
**Post date:** [October 13, 2022, 10:09am UTC](https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408/5 "2022-10-13T10:09:18Z")

</div>

What values to be added into the trusStorePath?

```auto
Path trustStorePath = Paths.get("/path/to/truststore.p12");
KeyStore truststore = KeyStore.getInstance("pkcs12");

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 14, 2022, 4:07am UTC](https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408/6 "2022-10-14T04:07:50Z")

</div>

I believe you need to follow the guide?

> **[Encrypted communication | Elasticsearch Java API Client \[8.4\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_encrypted_communication.html)**

---

<div class="post-metadata">

**Author:** ![Chirag\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chirag_gupta/32/111960_2.png) [@Chirag\_Gupta](https://discuss.elastic.co/u/Chirag_Gupta)\
**Post date:** [October 15, 2022, 5:40am UTC](https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408/7 "2022-10-15T05:40:03Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 12, 2022, 5:40am UTC](https://discuss.elastic.co/t/how-to-establish-a-connection-from-java-client-to-elastic-search-version-8/316408/8 "2022-11-12T05:40:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
