问题
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