# Declaring java client in elastic search?

**URL:** <https://discuss.elastic.co/t/declaring-java-client-in-elastic-search/88772>\
**Category:** Elasticsearch\
**Created:** [June 8, 2017, 11:57pm UTC](https://discuss.elastic.co/t/declaring-java-client-in-elastic-search/88772 "2017-06-08T23:57:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaushah](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@kaushah](https://discuss.elastic.co/u/kaushah)\
**Post date:** [June 8, 2017, 11:57pm UTC](https://discuss.elastic.co/t/declaring-java-client-in-elastic-search/88772/1 "2017-06-08T23:57:26Z")

</div>

How do I declare a java client?  
From the current documentation, it is done using  
// on startup

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)  
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))  
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));

// on shutdown

client.close();

What is host1 and host2 ?  
I get an error while running the java program. The error is:  
Exception in thread "main" java.net.UnknownHostException: host1: nodename nor servname provided, or not known  
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)  
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)  
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)  
at java.net.InetAddress.getAllByName0(InetAddress.java:1276)  
at java.net.InetAddress.getAllByName(InetAddress.java:1192)  
at java.net.InetAddress.getAllByName(InetAddress.java:1126)  
at java.net.InetAddress.getByName(InetAddress.java:1076)  
at Sherlock.ESTest1.javaApi.main(javaApi.java:21)

How do I resolve this?

---

<div class="post-metadata">

**Author:** ![kaushah](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@kaushah](https://discuss.elastic.co/u/kaushah)\
**Post date:** [June 9, 2017, 12:12am UTC](https://discuss.elastic.co/t/declaring-java-client-in-elastic-search/88772/2 "2017-06-09T00:12:49Z")

</div>

I'm using a single node cluster.  
By changing host1 to 127.0.0.1, the program worked.  
I believe, host1 and host2 are the names of the nodes.

PS: If anyone has the documentation on how to included more nodes, please let me know.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [June 9, 2017, 6:01am UTC](https://discuss.elastic.co/t/declaring-java-client-in-elastic-search/88772/3 "2017-06-09T06:01:33Z")

</div>

You can define explicit hosts in your TransportClient by calling  
the addTransportAddress method as highlighted in the documentation:  
[https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html)

However, if you are getting an UnknownHostException, that means your name  
server is not resolving the name. Depending on your OS, you can use system  
tools to verify the host name:

% nslookup host

---

<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:** [July 7, 2017, 6:01am UTC](https://discuss.elastic.co/t/declaring-java-client-in-elastic-search/88772/4 "2017-07-07T06:01:40Z")

</div>

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