# UnknownHostException while connecting to Elasticsearch

**URL:** <https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770>\
**Category:** Elasticsearch\
**Created:** [May 27, 2022, 6:31am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770 "2022-05-27T06:31:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enomine](https://avatars.discourse-cdn.com/v4/letter/e/f19dbf/32.png) [@Enomine](https://discuss.elastic.co/u/Enomine)\
**Post date:** [May 27, 2022, 6:31am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/1 "2022-05-27T06:31:55Z")

</div>

Hi,  
while doing my first steps with the tutorial

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

i face much problems even executing the first lines of code.  
I get an java.net.UnknownHostException that says "Der angegebene Host ist unbekannt ([https://xxx.yyyzzz.com](https://xxx.yyyzzz.com))" which translates to "The specified host is unknown ([https://xxx.yyyzzz.com](https://xxx.yyyzzz.com))".  
I replaced our companies url with xxx.yyyzzz.

 ![Screenshot 2022-05-27 081134](https://us1.discourse-cdn.com/elastic/original/3X/d/2/d223ca1a01bf4d6ded2e60b349bd96eafb5814bb.png)

But actually the server is reachable in the browser, and ping is okay, too:

 ![Screenshot 2022-05-27 082659](https://us1.discourse-cdn.com/elastic/original/3X/4/0/401d9984158a788760dcbb2471045b17c348915e.png)

How to solve?

Thanks - Enomine

---

<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:** [May 28, 2022, 6:49am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/2 "2022-05-28T06:49:58Z")

</div>

Welcome!

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.

Here a first guess is that you might have a firewall or a proxy?

---

<div class="post-metadata">

**Author:** ![Enomine](https://avatars.discourse-cdn.com/v4/letter/e/f19dbf/32.png) [@Enomine](https://discuss.elastic.co/u/Enomine)\
**Post date:** [May 30, 2022, 6:25am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/3 "2022-05-30T06:25:40Z")

</div>

I can't find any edit-Button, but here we go:

```auto
public class ConnectingTest {

	public void createClient() throws Exception {
		// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html

		// Create the low-level client
		RestClient restClient = RestClient.builder(new HttpHost("https://gattinger-second.es.europe-west1.gcp.cloud.es.io", 9243)).build();

		// Create the transport with a Jackson mapper
		ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

		// And create the API client
		ElasticsearchClient client = new ElasticsearchClient(transport);

		SearchResponse<Product> search = client.search(s -> s.index("products").query(q -> q.term(t -> t.field("name").value(v -> v.stringValue("bicycle")))), Product.class);

		// SearchResponse<Product> search2 = client.search

		for (Hit<Product> hit : search.hits().hits()) {
			processProduct(hit.source());
		}

	}

	private void processProduct(Product p) {
		System.out.println("ConnectingText -> process Product -> " + p.getName() + " " + p.getPrice() + " " + p.getSku() + " " + p.getClass());
	}

}

```

```auto
/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package usage;

public class Product {
	private String	sku;
	private String	name;
	private double	price;

	public Product() {
	}

	public Product(String sku, String name, double price) {
		this.sku = sku;
		this.name = name;
		this.price = price;
	}

	public String getSku() {
		return sku;
	}

	public void setSku(String sku) {
		this.sku = sku;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return this.price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
}

```

```auto
Exception in thread "main" java.io.IOException: Der angegebene Host ist unbekannt (https://gattinger-second.es.europe-west1.gcp.cloud.es.io)
	at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:923)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:299)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:287)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:146)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1502)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1519)
	at getting_started.ConnectingTest.createClient(ConnectingTest.java:53)
	at main.Main.main(Main.java:9)
Caused by: java.net.UnknownHostException: Der angegebene Host ist unbekannt (https://gattinger-second.es.europe-west1.gcp.cloud.es.io)
	at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
	at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:933)
	at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1519)
	at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:852)
	at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1509)
	at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1367)
	at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1301)
	at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
	at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:664)
	at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:635)
	at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:474)
	at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:280)
	at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:295)
	at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:377)
	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:129)
	at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:141)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:295)
	... 6 more

```

Thanks - Enomine

---

<div class="post-metadata">

**Author:** ![Enomine](https://avatars.discourse-cdn.com/v4/letter/e/f19dbf/32.png) [@Enomine](https://discuss.elastic.co/u/Enomine)\
**Post date:** [May 30, 2022, 6:28am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/4 "2022-05-30T06:28:27Z")

</div>

Solution:

```auto
RestClient restClient = RestClient.builder(new HttpHost("gattinger-second.es.europe-west1.gcp.cloud.es.io", 9243, "https")).build();

```

Putting https as a parameter.

Then we come 1 line ahead:

```auto
Exception in thread "main" co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [security_exception] missing authentication credentials for REST request [/products/_search?typed_keys=true]
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:281)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1502)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1519)
	at getting_started.ConnectingTest.createClient(ConnectingTest.java:53)
	at main.Main.main(Main.java:9)

```

Failure in Line 147 instead of 146 😉

Thanks - Enomine

---

<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:** [May 30, 2022, 10:08am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/5 "2022-05-30T10:08:25Z")

</div>

I'd recommend using:

```auto
RestClient restClient = RestClient.builder(HttpHost.create("https://gattinger-second.es.europe-west1.gcp.cloud.es.io:9243")).build();

```

That could make your life easier 😉

---

<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:** [May 30, 2022, 10:11am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/6 "2022-05-30T10:11:53Z")

</div>

The next problem is that you need to provide some credentials.  
Like:

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

RestClient restClient = RestClient.builder(HttpHost.create("https://gattinger-second.es.europe-west1.gcp.cloud.es.io:9243"))
                .setHttpClientConfigCallback(hcb -> hcb.setDefaultCredentialsProvider(credentialsProvider))
                .build();

```

Something like this.  
have a look at [Basic authentication | Elasticsearch Java API Client [8.2] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_basic_authentication.html)

---

<div class="post-metadata">

**Author:** ![Enomine](https://avatars.discourse-cdn.com/v4/letter/e/f19dbf/32.png) [@Enomine](https://discuss.elastic.co/u/Enomine)\
**Post date:** [June 1, 2022, 7:49am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/7 "2022-06-01T07:49:13Z")

</div>

Hi,  
thanks for your help. Got it to work right now:

```auto
public void createClient() throws Exception {
		// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html
		// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_basic_authentication.html

		final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme"));
		HttpClientConfigCallback httpClientConfigCallback = new HttpClientConfigCallback() {
			@Override
			public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
				httpClientBuilder.disableAuthCaching();
				return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
			}
		};

		// Create the low-level client
		RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("gattinger-second.es.europe-west1.gcp.cloud.es.io", 9243, "https"));
		restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
		RestClient restClient = restClientBuilder.build();
		

		// Create the transport with a Jackson mapper
		ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

		// And create the API client
		ElasticsearchClient client = new ElasticsearchClient(transport);

		SearchResponse<Product> search = client.search(s -> s.index("products").query(q -> q.term(t -> t.field("name").value(v -> v.stringValue("bicycle")))), Product.class);
		System.out.println("SearchResponse erfolgreich. Anzahl: " + search.hits().hits().size());

```

Thanks - Enomine

---

<div class="post-metadata">

**Author:** ![Enomine](https://avatars.discourse-cdn.com/v4/letter/e/f19dbf/32.png) [@Enomine](https://discuss.elastic.co/u/Enomine)\
**Post date:** [June 1, 2022, 8:17am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/8 "2022-06-01T08:17:19Z")

</div>

Do you actually know why the program is not terminating after this code? In eclipse it stays as running until i hit the stop button.

Thanks - Enomine

---

<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:** [June 1, 2022, 9:17am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/9 "2022-06-01T09:17:09Z")

</div>

Probably because you did not close the client?

---

<div class="post-metadata">

**Author:** ![Enomine](https://avatars.discourse-cdn.com/v4/letter/e/f19dbf/32.png) [@Enomine](https://discuss.elastic.co/u/Enomine)\
**Post date:** [June 1, 2022, 11:04am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/10 "2022-06-01T11:04:52Z")

</div>

Oh! right, thanks!

Enomine

---

<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:** [June 29, 2022, 11:05am UTC](https://discuss.elastic.co/t/unknownhostexception-while-connecting-to-elasticsearch/305770/11 "2022-06-29T11:05:42Z")

</div>

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