# Java problem: InetAddress.getLocalHost is not working



## hannibal80 (Sep 25, 2011)

Hello all, 
I'm a FreeBSD newbie as you can see from my presentation post. I hope this is the right place to post this thread. 

I'm trying to make http://www.serviio.org/ works (it's a DLNA server made in Java) but I always had the same problem.

So, I'm asking you if someone can help me dealing with this. 
The problem is, as you can see from serviio log, that the call to 
	
	



```
InetAddress.getLocalHost
```
 gives an UnknownHostException:


```
2011-09-25 18:42:23,358 ERROR [ApplicationInstanceManager] :
java.net.UnknownHostException: :
        at java.net.InetAddress.getLocalHost(InetAddress.java:1353)
        at org.serviio.ApplicationInstanceManager.registerInstance(ApplicationInstanceManager.java:68)
        at org.serviio.MediaServer.checkForRunningInstances(MediaServer.java:230)
        at org.serviio.MediaServer.main(MediaServer.java:89)
2011-09-25 18:42:23,370 INFO  [MediaServer] ------------------------------------------------------------------------
2011-09-25 18:42:23,370 INFO  [MediaServer] Serviio DLNA media streaming server v 0.5.2 (rev. 4e3210bd9ff3)
2011-09-25 18:42:23,370 INFO  [MediaServer] Petr Nejedly 2009-2011
2011-09-25 18:42:23,370 INFO  [MediaServer] http://www.serviio.org
2011-09-25 18:42:23,370 INFO  [MediaServer]
2011-09-25 18:42:23,371 INFO  [MediaServer] Java 1.6.0_03-p4-Sun Microsystems Inc.
2011-09-25 18:42:23,371 INFO  [MediaServer] OS FreeBSD amd64 8.2-RELEASE
2011-09-25 18:42:23,371 INFO  [MediaServer] File encoding: ISO-8859-1
2011-09-25 18:42:23,371 INFO  [MediaServer] ------------------------------------------------------------------------
2011-09-25 18:42:23,533 INFO  [RestletServer] Starting Restlet server exposed on localhost:23423
2011-09-25 18:42:23,613 INFO  [DBSchemaUpdateExecutor] Checking if DB schema needs to be updated
2011-09-25 18:42:23,621 DEBUG [DBLogDAOImpl] Checking if script 'script-0.1.sql' has been run
2011-09-25 18:42:25,074 DEBUG [DBLogDAOImpl] Checking if script 'script-0.1.1.sql' has been run
2011-09-25 18:42:25,075 DEBUG [DBLogDAOImpl] Checking if script 'script-0.2.sql' has been run
2011-09-25 18:42:25,076 DEBUG [DBLogDAOImpl] Checking if script 'script-0.3.1.sql' has been run
2011-09-25 18:42:25,077 DEBUG [DBLogDAOImpl] Checking if script 'script-0.4.sql' has been run
2011-09-25 18:42:25,078 DEBUG [DBLogDAOImpl] Checking if script 'script-0.4.1.sql' has been run
2011-09-25 18:42:25,079 DEBUG [DBLogDAOImpl] Checking if script 'script-0.4.2.sql' has been run
2011-09-25 18:42:25,080 DEBUG [DBLogDAOImpl] Checking if script 'script-0.5.sql' has been run
2011-09-25 18:42:25,082 DEBUG [DBLogDAOImpl] Checking if script 'script-0.5.1.sql' has been run
2011-09-25 18:42:25,089 WARN  [ProfilesDefinitionParser] Cannot get name of the local computer: :
```

I tried with three 1.6 jdks, OpenJDK, DiabloJDK and OracleJDK, still the same problem. 

I also made a simple java class packaged in a runnable jar to check if the problem was on Serviio or on my system and the answer is... the second. 

This is my java class:

```
import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressTest {
	public static void main(String[] args) {
		String localHostName = "no name";
		String localHostAddress = "no address";
		try {
			localHostName = InetAddress.getLocalHost().getHostName();
			System.out.println("localHostName: " + localHostName);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			System.out.println("No name: " + e.getMessage());
		}
		try {
			localHostAddress = InetAddress.getLocalHost().getHostAddress();
			System.out.println("localHostAddress: " + localHostAddress);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			System.out.println("No address: " + e.getMessage());
		}
	}
}
```

and this is the output:


```
# java -jar /usr/home/Lorenzo/Desktop/Download/java/test/InetAddressTest.jar
java.net.UnknownHostException: :
        at java.net.InetAddress.getLocalHost(InetAddress.java:1353)
        at InetAddressTest.main(InetAddressTest.java:15)
No name: :
java.net.UnknownHostException: :
        at java.net.InetAddress.getLocalHost(InetAddress.java:1353)
        at InetAddressTest.main(InetAddressTest.java:23)
No address: :
```

The java version is:

```
# java -version
java version "1.6.0_03-p4"
Java(TM) SE Runtime Environment (build 1.6.0_03-p4-Lorenzo_25_sep_2011_16_30-b00)
Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_03-p4-Lorenzo_25_sep_2011_16_30-b00, mixed mode)
```

I read about the hosts file so I went looking around in /etc and I found tho files, /etc/host and /etc/hosts that I show you:

```
# more /etc/host
# $FreeBSD$
#
# Host Database
# Questo file dovrebbe contenere gli indirizzi e gli alias
# per gli host locali che condividono questo file.
# In presenza di DNS o NIS, questo file potrebbe non essere consultato affatto;
# guarda /etc/nsswitch.conf per l'ordine di risoluzione.
#
#[Indirizzo Internet ] [nome host ufficiale] [alias1] [alias2] ...
#

::1                     localhost
127.0.0.1               localhost FreeBSD
192.168.0.160           localhost FreeBSD

#
# Rete immaginaria.
#10.0.0.2               myname.my.domain myname
#10.0.0.3               myfriend.my.domain myfriend
#
# In accordo all'RFC 1918, puoi usare le seguenti classi di IP per reti private
# che non verranno mai connesse ad Internet:
#
#       10.0.0.0        -   10.255.255.255
#       172.16.0.0      -   172.31.255.255
#       192.168.0.0     -   192.168.255.255
#
# In caso volessi essere in grado di collegarti ad Internet, avrai bisogno
# di veri numeri ufficiali assegnati.  PER FAVORE PER FAVORE PER FAVORE
# non tentare di inventarti i numeri della tua rete ma fattene assegnare
# uno dal tuo provider (se ne hai uno) o dall'Internet Registry (ftp su
# rs.internic.net, directory `/templates').
```
and

```
# more /etc/hosts
# $FreeBSD: src/etc/hosts,v 1.16.34.1.6.1 2010/12/21 17:09:25 kensmith Exp $
#
# Host Database
#
# This file should contain the addresses and aliases for local hosts that
# share this file.  Replace 'my.domain' below with the domainname of your
# machine.
#
# In the presence of the domain name service or NIS, this file may
# not be consulted at all; see /etc/nsswitch.conf for the resolution order.
#
#
::1                     localhost
127.0.0.1               localhost FreeBSD
192.168.0.160           localhost FreeBSD
#
# Imaginary network.
#10.0.0.2               myname.my.domain myname
#10.0.0.3               myfriend.my.domain myfriend
#
# According to RFC 1918, you can use the following IP networks for
# private nets which will never be connected to the Internet:
#
#       10.0.0.0        -   10.255.255.255
#       172.16.0.0      -   172.31.255.255
#       192.168.0.0     -   192.168.255.255
#
# In case you want to be able to connect to the Internet, you need
# real official assigned numbers.  Do not try to invent your own network
# numbers but instead get one from your network provider (if any) or
# from your regional registry (ARIN, APNIC, LACNIC, RIPE NCC, or AfriNIC.)
#
```

I don't know why the firs has Italian comments, anyway the IP address of my box is 192.168.0.160.

What should I do? Have you got any idea?

Thanks a lot.


----------



## plamaiziere (Sep 25, 2011)

hannibal80 said:
			
		

> The problem is, as you can see from serviio log, that the call to
> 
> 
> 
> ...



Is your jvm built with ipv6 enabled? I've got many problems with this (not only with FreeBSD).

Try to run your java program with the jvm flag -Djava.net.preferIPv4Stack=true

Regards.


----------



## hannibal80 (Sep 25, 2011)

Thanks plamaiziere but yes, the serviio daemon script already has the -Djava.net.preferIPv4Stack=true flag.

I tried to call my jar with the same flag but nothing seems to change.

Is there any way to check if my hosts file is working?


----------



## hannibal80 (Sep 25, 2011)

I think that this demonstrate that my hosts file it's working


```
# ping FreeBSD
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.030 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.045 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.070 ms
^C
--- localhost ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.030/0.048/0.070/0.016 ms
# ping localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.029 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.047 ms
^C
--- localhost ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.029/0.038/0.047/0.009 ms
# ping 192.168.0.160
PING 192.168.0.160 (192.168.0.160): 56 data bytes
64 bytes from 192.168.0.160: icmp_seq=0 ttl=64 time=0.030 ms
64 bytes from 192.168.0.160: icmp_seq=1 ttl=64 time=0.050 ms
^C
--- 192.168.0.160 ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.030/0.040/0.050/0.010 ms
```


----------



## plamaiziere (Sep 26, 2011)

hannibal80 said:
			
		

> This is my java class:
> 
> ```
> import java.net.InetAddress;
> ...



Well, I've tried your code and it works fine for me (openjdk 1.6/ freebsd 8.2/32 bits).

I don't see why it does not work for you. May be you can try to download the source of the jdk and run your code in a debuguer ?

Regards.


----------



## hannibal80 (Sep 26, 2011)

Well, it could be a good idea. I already checked the java classes in the jdk 1.6 and I found that call is native method so seems that there is some other code that is not java and that I don't know.

Anyway I could try installing an IDE on my FreeBSD desktop. I hope it will be not so hard...

Thank you plamaiziere.


----------



## Sumana (Nov 14, 2011)

Hello Hannibal,

   Am also facing the UnknownhostException issues with InetAddress.getLocalHost. 

java version used: 1.6.0_04-b12.
Operating System details: x86_64 GNU/Linux

But one difference is from your post is:
 - The stan alone application containing this method invocation is running fine. But with the actual application, when ran in debug mode, its giving problem in the line where this method is invoked.

   So if you can share any further information would be helpful.

regards
 Sumana


----------



## hannibal80 (Nov 14, 2011)

I'm sorry Sumana but I didn't find any solution for it. 

So I made a complete reinstall of the system and now it's working properly. 

Did you try my simple code?


----------



## SirDice (Nov 14, 2011)

Are you trying to run the code inside a jail? A jail doesn't have access to lo0 or 127.0.0.1.


----------



## Sumana (Nov 14, 2011)

Hello Hannibal, SirDice,

  Thank you for the quick reply. It really encourages as am totally lost with the current scenario.



> Did you try my simple code?


   - Had tried similar (I mean only differs in classname!) program before posting this query. But strange thing is that it works on the same hardware and jre all the time.



> Are you trying to run the code inside a jail?


   - As am not aware of jail, i have to first study this and then check whether the current environment has such environment. As it is some customer integration and am not aware how its done there. Will check this any how.

But some more things to share:
The application which is having the issue is very old product and deployed in many places. And this issue is faced only with this hardware and jre combination. And even this is "*sporadic*" issue, i.e some times even in this setup the application works fine without UnknownHostException!

regards
 Sumana


----------



## plamaiziere (Nov 14, 2011)

Sumana said:
			
		

> The application which is having the issue is very old product and deployed in many places. And this issue is faced only with this hardware and jre combination. And even this is "*sporadic*" issue, i.e some times even in this setup the application works fine without UnknownHostException !!!!!



I don't understand why you have defined 
	
	



```
192.168.0.160 localhost
```
 in your /etc/hosts. Try to remove it.

I've tested your code that works fine here. Don't see why it doesn't work for you. 

Regards.


----------



## hannibal80 (Nov 15, 2011)

plamaiziere said:
			
		

> I don't understand why you have defined
> 
> 
> 
> ...



Hi Plamaiziere, 
that was my old configuration, when I was trying almost everything to make it working, not the one from Sumana


----------

