OpenJDK 8: can't resolve hostname

帅比萌擦擦* 提交于 2019-12-13 15:21:52

问题


I'm trying to run slf4j with log4j 2.8 on openJDK 8 (latest version on Ubuntu 8u131-b11-0ubuntu1.17.04.1).

When running simple java class via maven openjdk is unable to resolve local hostname:

  Exception in thread "AWT-EventQueue-0" java.lang.NoSuchFieldError: preferIPv6Address
    at java.base/java.net.InetAddress.init(Native Method)
    at java.base/java.net.InetAddress.init(Native Method)
    at java.base/java.net.InetAddress.<clinit>(InetAddress.java:333)
    at org.apache.logging.log4j.core.util.NetUtils.getLocalHostname(NetUtils.java:53)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:539)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:122)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:43)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)

looks like the method

public static InetAddress getLocalHost() throws UnknownHostException 

is implemented differently than in Oracle java.

/etc/hosts contains ipv6 record:

::1     ip6-localhost ip6-loopback 

回答1:


The problem was caused by JDK9 installed on the system. Log4j 2.8 is not compatible with Java 9 yet.

Either removing JDK9 or setting preference to JDK8 should solve the issue. On Debian:

sudo update-alternatives --config java


来源:https://stackoverflow.com/questions/44167296/openjdk-8-cant-resolve-hostname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!