# Java connection lost

**URL:** <https://discuss.elastic.co/t/java-connection-lost/266454>\
**Category:** Elasticsearch\
**Created:** [March 6, 2021, 9:06pm UTC](https://discuss.elastic.co/t/java-connection-lost/266454 "2021-03-06T21:06:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cosmin\_Ciobanu1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cosmin_ciobanu1/32/78785_2.png) [@Cosmin\_Ciobanu1](https://discuss.elastic.co/u/Cosmin_Ciobanu1)\
**Post date:** [March 6, 2021, 9:06pm UTC](https://discuss.elastic.co/t/java-connection-lost/266454/1 "2021-03-06T21:06:17Z")

</div>

Hi!  
I'm trying to connect to my Elasticsearch node from Java.  
I have this code :

```auto
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("name", "pass"));

            Path trustStorePath = Paths.get("/home/name/certCA.p12");
            KeyStore truststore = KeyStore.getInstance("pkcs12");

            try (InputStream is = Files.newInputStream(trustStorePath)) {
                truststore.load(is, null);
            }

            SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);
            final SSLContext sslContext = sslBuilder.build();

            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("10.10.10.10", 9200, "https")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

                        public HttpAsyncClientBuilder customizeHttpClient(
                                final HttpAsyncClientBuilder httpAsyncClientBuilder) {

                            return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext);

                        }

                    })

            );
            if(client.ping(RequestOptions.DEFAULT)){
                System.out.println("Connected");
            }

```

After I run it I get error with connection is closed.  
Any idea?  
Thank you!

---

<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:** [April 3, 2021, 9:07pm UTC](https://discuss.elastic.co/t/java-connection-lost/266454/2 "2021-04-03T21:07:15Z")

</div>

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