OpenJDK 8:无法解析主机名

时间:2017-05-24 19:48:55

标签: java java-8 openjdk

我尝试在openJDK 8上使用slf4j 2.8运行log4j(Ubuntu 8u131-b11-0ubuntu1.17.04.1上的最新版本)。

当通过maven运行简单的java类时,openjdk无法解析本地主机名:

  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)

看起来像方法

public static InetAddress getLocalHost() throws UnknownHostException 

的实现方式与Oracle java中的实现方式不同。

/etc/hosts包含ipv6记录:

::1     ip6-localhost ip6-loopback 

1 个答案:

答案 0 :(得分:2)

问题是由系统上安装的JDK9引起的。 Log4j 2.8尚未与Java 9兼容。

删除JDK9或将设置首选项设置为JDK8应该可以解决问题。关于Debian:

sudo update-alternatives --config java