Recently while running Java Program on my Mac OS X, I noticed below error in Eclipse console.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
java
.
net
.
UnknownHostException
:
LAPTOP
-
23876346
:
LAPTOP
-
23876346
:
nodename
nor
servname
provided
,
or
not
known
at
java
.
net
.
InetAddress
.
getLocalHost
(
InetAddress
.
java
:
1473
)
at
org
.
eclipse
.
rse
.
core
.
RSECorePlugin
.
getLocalMachineName
(
RSECorePlugin
.
java
:
265
)
at
org
.
eclipse
.
rse
.
core
.
RSEPreferencesManager
.
getDefaultPrivateSystemProfileName
(
RSEPreferencesManager
.
java
:
358
)
at
org
.
eclipse
.
rse
.
core
.
RSEPreferencesManager
.
initDefaults
(
RSEPreferencesManager
.
java
:
337
)
at
org
.
eclipse
.
rse
.
internal
.
core
.
RSEPreferenceInitializer
.
initializeDefaultPreferences
(
RSEPreferenceInitializer
.
java
:
23
)
at
org
.
eclipse
.
core
.
internal
.
preferences
.
PreferenceServiceRegistryHelper
$
1.run
(
PreferenceServiceRegistryHelper
.
java
:
300
)
at
org
.
eclipse
.
core
.
runtime
.
SafeRunner
.
run
(
SafeRunner
.
java
:
42
)
.
.
.
.
.
.
.
.
.
.
.
.
Caused
by
:
java
.
net
.
UnknownHostException
:
LAPTOP
-
23876346
:
nodename
nor
servname
provided
,
or
not
known
at
java
.
net
.
Inet6AddressImpl
.
lookupAllHostAddr
(
Native
Method
)
at
java
.
net
.
InetAddress
$
1.lookupAllHostAddr
(
InetAddress
.
java
:
901
)
at
java
.
net
.
InetAddress
.
getAddressesFromNameService
(
InetAddress
.
java
:
1293
)
at
java
.
net
.
InetAddress
.
getLocalHost
(
InetAddress
.
java
:
1469
)
.
.
.
28
more
|
I was literally scratching my head as I didn’t see this error anywhere on my Windows Desktop duringJava development. What causes the error – java.net.UnknownHostException – How to solve UnknownHostException?
While debugging I came to know that – this came from my below Java code:
1
2
3
4
5
6
7
8
9
|
public
static
String
returnHostName
(
)
{
String
crunchifyHost
=
null
;
try
{
crunchifyHost
=
InetAddress
.
getLocalHost
(
)
.
getCanonicalHostName
(
)
;
// This line was throwing above exception
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"Error. Failed to retrive crunchifyHost:"
+
e
)
;
}
return
crunchifyHost
;
}
|
Sometimes it’s very annoying to get exception on one Operating System compare to other.getCanonicalHostName()
returns the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN (Fully Qualified Domain Name) depending on the underlying system configuration. Mac OS X uses the different system configuration than windows.
Step 1. Open Terminal Windows.
Step 2. Enter command sudo vi /private/etc/hosts
Step 3. You should see file content like that.
Step 4. Add below line 127.0.0.1 LAPTOP-23876346
immediately after 127.0.0.1 localhost
1
2
3
4
5
6
7
8
9
10
|
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1
localhost
127.0.0.1
LAPTOP
-
23876346
# added line. replace LAPTOP-23876346 with your laptop hostname
255.255.255.255
broadcasthost
::
1
localhost
|
Step 5. Execute below command to flush the cache
1
|
bash
-
3.2
$
dscacheutil
-
flushcache
|
Now again run the same java project and you should NOT seejava.net.UnknownHostException
again.