1. 程式人生 > >Tomcat在Linux環境下啟動異常 java.net.UnknownHostException: HP_MAIN12: HP_MAIN12

Tomcat在Linux環境下啟動異常 java.net.UnknownHostException: HP_MAIN12: HP_MAIN12

剛開發完一個專案,在本地部署的時候沒有任何異常,到Linux系統就出現這樣那樣的問題,下面就是其中一個:
Tomcat在Linux平臺下啟動的時候丟擲java.net.UnknownHostException: HP_MAIN12: HP_MAIN12異常:
2014-01-09 11:04:26,609  WARN (ConfigurationFactory.java:136) No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/home/jbxx/product/apache-tomcat-6.0.35/webapps/infocms/WEB-INF/lib/ehcache-core-2.6.3.jar!/ehcache-failsafe.xml
2014-01-09 11:04:26,696 ERROR (Cache.java:215) Unable to set localhost. This prevents creation of a GUID. Cause was: HP_MAIN12: HP_MAIN12:

java.net.UnknownHostException: HP_MAIN12: HP_MAIN12
    at java.net.InetAddress.getLocalHost(InetAddress.java:1426)
    at net.sf.ehcache.Cache.<clinit>(Cache.java:213)
    at net.sf.ehcache.config.ConfigurationHelper.createCache(ConfigurationHelper.java:296)
    at net.sf.ehcache.config.ConfigurationHelper.createDefaultCache(ConfigurationHelper.java:219)
    at net.sf.ehcache.CacheManager.configure(CacheManager.java:722)
    at net.sf.ehcache.CacheManager.doInit(CacheManager.java:439)
    at net.sf.ehcache.CacheManager.init(CacheManager.java:377)
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:259)
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:1037)
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:818)
    at net.sf.ehcache.CacheManager.create(CacheManager.java:799)
    at net.sf.ehcache.CacheManager.getInstance(CacheManager.java:833)
    at org.springframework.cache.ehcache.EhCacheFactoryBean.afterPropertiesSet(EhCacheFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    ... ...
HP_MAIN12是你Linux機器名稱,這個異常好像對程式沒有多大影響。
異常出現的原因是在專案中引用的ehcache-core-2.6.3.jar快取包,快取需要將資料存放在本機,則使用了InetAddress.getLocalHost()獲得本機方法。在Linux作業系統中,都是以/etc/hosts中的配置查詢主機名的,使用hostname檢視本機名稱,若本機名稱不是一個IP地址, 比如是"sohu",則必須在/etc/hosts中配置sohu對應本機IP,否則java.net.InetAddress.getLocalHost會丟擲java.net.UnknownHostException異常。
解決的辦法也很簡單:
在/etc/hosts里加一行 本機IP mName

127.0.0.1   HP_MAIN12
問題解決。