BajajG
BajajG

Reputation: 2174

String hostname from properties file: Java

This may sound like a really simple problem but I can't figure out a way around this.

I have a config.properties file which contains two key values: an IP address and a Port number. I read this config file to extract the key values in string format. However, when I am trying to use these values, I am unable to connect to the IP address retrieved from the config file.

The reason is that the values read are in string format and I need to convert them to proper formats before using them. What I want is that the value "192.168.1.40" stored in config file is converted to a String host format. I tried using InetAddress but that gives an error. The contents of the config file are:

IP=192.168.1.40
PORT=9124

The code that uses these values is as follows:

Properties prop = new Properties();
String propFileName = "...//testJedis//resources//config.properties";        
prop.load(new FileInputStream(propFileName));//testJedis/resources/config.properties"));// configStream);

Jedis jedis=new Jedis(prop.getProperty("IP"),Integer.parseInt(prop.getProperty("PORT")));
//Jedis jedis = new Jedis("192.168.1.40",9124);

The error stack I obtain is as follows:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException: "192.168.1.40"
at redis.clients.jedis.Connection.connect(Connection.java:150)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:71)
at redis.clients.jedis.Connection.sendCommand(Connection.java:92)
at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:84)
at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:65)
at TestJedis.main(TestJedis.java:43)
Caused by: java.net.UnknownHostException: "192.168.1.40"
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at redis.clients.jedis.Connection.connect(Connection.java:144)
... 5 more

The documentation of jedis class can be found here!

P.S.: If I directly pass the host address as string, it works properly (that means the host is perfectly reachable).

Upvotes: 2

Views: 5465

Answers (1)

Akash Thakare
Akash Thakare

Reputation: 23012

java.net.UnknownHostException: "192.168.1.40"
                               ^            ^

IMHO this double quotes shouldn't be there as this Exception takes String as an argument of hostName which is unknown. The only reason of this Exception can be that you are actually passing double quotes in hostName.

It should be java.net.UnknownHostException: 192.168.12<--If host is unknown

From Comment

IP is returned as "192.168.1.40" (quotes included) when I print them on screen

Which simply means your String IP="\"192.168.1.40\"" not String IP="192.168.1.40" remove double quotes from configuration file.

I am unable to reproduce the issue but you can just use replace

String ip=prop.getProperty("IP").replace("\"","")

Upvotes: 1

Related Questions