# Connect to Elasticsearch using Java High Level Client

**URL:** <https://discuss.elastic.co/t/connect-to-elasticsearch-using-java-high-level-client/177917>\
**Category:** Elasticsearch\
**Created:** [April 23, 2019, 1:02am UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-using-java-high-level-client/177917 "2019-04-23T01:02:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kurezza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kurezza/32/44607_2.png) [@Kurezza](https://discuss.elastic.co/u/Kurezza)\
**Post date:** [April 23, 2019, 1:02am UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-using-java-high-level-client/177917/1 "2019-04-23T01:02:44Z")

</div>

Hi, I'm new to Elasticsearch. I'm trying to connect Java to Elasticsearch using Java High Level Client. However, I always got an IOException. I was sure that my Elasticsearch is up and running. Here is the code I used. My Elasticsearch and high level client are both in version 7.0.0

```
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "mypassword"));

        RestClientBuilder builder = RestClient
                .builder(new HttpHost("myclustername.us-east-1.aws.found.io", 9243, "http"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        RestHighLevelClient client = new RestHighLevelClient(builder);
        GetRequest getRequest = new GetRequest("item", "1");
 

try {
    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
    e.printStackTrace();
}

```

Below is the stack trace:  
2019-04-23T01:12:08.072466+00:00 app[web.1]: org.apache.http.ConnectionClosedException: Connection is closed

```
2019-04-23T01:12:08.074210+00:00 app[web.1]: at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:794)

2019-04-23T01:12:08.074276+00:00 app[web.1]: at org.elasticsearch.client.RestClient.performRequest(RestClient.java:225)

2019-04-23T01:12:08.074352+00:00 app[web.1]: at org.elasticsearch.client.RestClient.performRequest(RestClient.java:212)

2019-04-23T01:12:08.074422+00:00 app[web.1]: at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1433)

2019-04-23T01:12:08.074492+00:00 app[web.1]: at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1403)

2019-04-23T01:12:08.074571+00:00 app[web.1]: at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1373)

2019-04-23T01:12:08.074637+00:00 app[web.1]: at org.elasticsearch.client.RestHighLevelClient.get(RestHighLevelClient.java:699)

2019-04-23T01:12:08.074702+00:00 app[web.1]: at edu.lehigh.cse280.swap.app.App.main(App.java:110)

2019-04-23T01:12:08.074855+00:00 app[web.1]: Caused by: org.apache.http.ConnectionClosedException: Connection is closed

2019-04-23T01:12:08.074947+00:00 app[web.1]: at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:356)

2019-04-23T01:12:08.075024+00:00 app[web.1]: at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:261)

2019-04-23T01:12:08.075099+00:00 app[web.1]: at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)

2019-04-23T01:12:08.075165+00:00 app[web.1]: at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)

2019-04-23T01:12:08.075233+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)

2019-04-23T01:12:08.075308+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)

2019-04-23T01:12:08.077773+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)

2019-04-23T01:12:08.077840+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)

2019-04-23T01:12:08.077930+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)

2019-04-23T01:12:08.078001+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)

2019-04-23T01:12:08.078073+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)

2019-04-23T01:12:08.078138+00:00 app[web.1]: at java.lang.Thread.run(Thread.java:748)

```

I tried with curl command  
`curl -X GET -u elastic:mypassword "https://myclustername.us-east-1.aws.found.io:9243/item/_doc/1"`  
And it worked well.

This is my first time posting, please forgive me if my question is formatted incorrectly 🙂

Thanks!

---

<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:** [April 23, 2019, 4:49am UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-using-java-high-level-client/177917/2 "2019-04-23T04:49:33Z")

</div>

Try with `https` instead of `http`.

---

<div class="post-metadata">

**Author:** ![Kurezza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kurezza/32/44607_2.png) [@Kurezza](https://discuss.elastic.co/u/Kurezza)\
**Post date:** [April 23, 2019, 5:31am UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-using-java-high-level-client/177917/3 "2019-04-23T05:31:35Z")

</div>

OMG you are a life saver! This problem has bothered me for weeks! Thank you very much!

---

<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:** [May 21, 2019, 5:56am UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-using-java-high-level-client/177917/5 "2019-05-21T05:56:23Z")

</div>

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