Search code examples
javaredishostsjedis

java.net.UnknownHostException: "localhost": Name or service not known


I'm trying to connect to Redis in Java using Jedis. I'm doing this simply like

return new JedisPool(jedisPoolConfig,
                    redisData.get("address").toString(),
                    redisData.get("port").asInt(),
                    2000,
                    redisData.get("password").toString());

With {"address": "localhost", "port": 6363, "password": "password}

However, I am getting this error when trying to connect:

redis.clients.jedis.exceptions.JedisConnectionException: Failed to create socket.
    at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:116)
    at redis.clients.jedis.Connection.connect(Connection.java:180)
    at redis.clients.jedis.Connection.initializeFromClientConfig(Connection.java:338)
    at redis.clients.jedis.Connection.<init>(Connection.java:53)
    at redis.clients.jedis.Jedis.<init>(Jedis.java:212)
    at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:181)
    at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:571)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:298)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:223)
    at redis.clients.jedis.util.Pool.getResource(Pool.java:34)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:364)
    at org.myapp.monitors.Monitor$DefaultImpls.setRedis(Monitor.kt:13)
    at org.myapp.monitors.IntMonitor.setRedis(IntMonitor.kt:8)
    at org.myapp.monitors.IntMonitor.<init>(IntMonitor.kt:10)
    at org.myapp.monitors.IntMonitor.<init>(IntMonitor.kt:8)
    at org.myapp.monitors.MonitorFactory.makeMonitor(MonitorFactory.kt:22)
    at org.myapp.utils.ConfigUtils.getMonitors(ConfigUtils.java:62)
    at org.myapp.tp.TelPub.<clinit>(TelPub.kt:16)
Caused by: java.net.UnknownHostException: "localhost": Name or service not known
    at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:932)
    at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1517)
    at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:851)
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300)
    at redis.clients.jedis.DefaultJedisSocketFactory.connectToFirstSuccessfulHost(DefaultJedisSocketFactory.java:58)
    at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:87)
    ... 17 more
Caused by: java.net.UnknownHostException: "localhost"
    at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:800)
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300)
    at redis.clients.jedis.DefaultJedisSocketFactory.connectToFirstSuccessfulHost(DefaultJedisSocketFactory.java:58)
    at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:87)
    ... 17 more

Same thing happens if I replace "localhost" with "127.0.0.1" or "::1".

I'm on popOS 21.10 and this is my /etc/host file:

127.0.0.1   localhost
::1     localhost
127.0.1.1   pop-os.localdomain  pop-os

echo $HOST_NAME returns "thinkpad-t440p".

I've tried

127.0.0.1   thinkpad-t440p localhost
::1         localhost
127.0.1.1   pop-os.localdomain  pop-os

but same issue.

ping localhost gives me

PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.025 ms

ping thinkpad-t440p gives me

PING thinkpad-t440p (192.168.0.42) 56(84) bytes of data.
64 bytes from thinkpad-t440p (192.168.0.42): icmp_seq=1 ttl=64 time=0.027 ms

Solution

  • The problem was using the toString() method, which basically puts a string inside a string so it adds extra quotes to it or something.

    Need to use asString() instead.