Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Getting ‘java.net.UnknownHostException: LAPTOP-23876346: nodename nor servname provided, or not known’ Error on MAC OS X? Update Your /private/etc/hosts File

$
0
0

Recently while running Java Program on my Mac OS X, I noticed below error in Eclipse console.

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 during Java 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:

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.

Well, the solution is simple. Follow below steps and you should be good.

Step 1. Open Terminal Windows.

Step 2. Enter command sudo vi /private/etc/hosts

Step 3. You should see file content like that.

Crunchify private ect hosts file content Getting java.net.UnknownHostException: LAPTOP 23876346: nodename nor servname provided, or not known Error on MAC OS X? Update Your /private/etc/hosts File

Step 4. Add below line 127.0.0.1 LAPTOP-23876346 immediately after 127.0.0.1 localhost

##
# 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

bash-3.2$ dscacheutil -flushcache

Now again run the same java project and you should NOT see java.net.UnknownHostException again.

Have anything to add to this article? Please chime in and join the conversion.


The post Getting ‘java.net.UnknownHostException: LAPTOP-23876346: nodename nor servname provided, or not known’ Error on MAC OS X? Update Your /private/etc/hosts File appeared first on Crunchify.
Author: Arpit Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles