2

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).

8
  • Could you store the two properties read (the string and the int) in separate variables and log them on screen just to be sure? Commented Jun 27, 2014 at 9:55
  • 1
    Any whitespace characters sneaking in somewhere? Commented Jun 27, 2014 at 9:56
  • The stack says you have an UnknownHostException; its attempting to connect to that IP. Are you able to connect without the prop. file? Commented Jun 27, 2014 at 9:58
  • Use prop.getProperty("IP").trim() and try. Commented Jun 27, 2014 at 10:00
  • 1
    can you try creating a new file in notepad with your entries ? May be some char encoding causing issues. Commented Jun 27, 2014 at 10:29

1 Answer 1

1
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("\"","")
Sign up to request clarification or add additional context in comments.

4 Comments

The contents of the config file are mentioned in the question. I haven't added any quotes and I am still facing problem reading it.
Yes, that is exactly the problem. Even though I haven't added any quotes in the config file, I am not able to read the value as 'just a string'.
Is it happening for PORT also?
No, PORT is read absolutely fine. The problem is that when I am using "." in the value field, the format is changing. If I set the property using prop.setProperty("IP","192.168.1.40") the program works fine. I guess there is some proble using the "." in value. I tried using \ to escape the meaning but it does not help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.