Resolving Slow Java Application Startup on macOS Caused by a JDK getLocalHost Bug
The article describes a macOS JDK bug where calls to java.net.InetAddress.getLocalHost() cause a 5‑second (or longer) delay during application startup, explains the investigation steps, and provides three practical fixes including hosts file modification, interface aliasing, and JDK upgrade.
After upgrading to a newer Mac, the author noticed that while code compilation was fast, any use of log4j2 or additional functionality caused the application to start extremely slowly, often taking several seconds before any console output appeared.
Initial suspicion fell on log4j2 , but further investigation revealed that the delay originated from a call to InetAddress.getLocalHost().getHostAddress() inside a utility method getLocalIp() , which was taking about 5 seconds to return.
The author examined static initialization blocks that created log directories and performed file cleanup, but the problem persisted even after commenting out those sections. The delay was traced to the JDK's handling of host name resolution on macOS.
/**
* 创建日志文件夹和数据存储文件夹
*/
static {
new File(LOG_Path).mkdir();
new File(LONG_Path).mkdir();
File file = new File(REQUEST_Path);
File mark = new File(MARK_Path);
File data = new File(DATA_Path);
file.mkdir();
mark.mkdir();
data.mkdir();
List
allFile = FileUtil.getAllFile(DATA_Path);
allFile.addAll(FileUtil.getAllFile(MARK_Path));
allFile.addAll(FileUtil.getAllFile(REQUEST_Path));
allFile.stream().map(y -> new File(y)).forEach(x -> {
if (Time.getTimeStamp() - x.lastModified() > 3 * DAY) x.delete();
});
logger.info("当前用户:{},IP:{},工作目录:{},系统编码格式:{},系统{}版本:{}", COMPUTER_USER_NAME, LOCAL_IP, WORK_SPACE, SYS_ENCODING, SYS_NAME, SYS_VERSION);
}The problematic method:
/**
* 获取本机IP
*
* @return
*/
public static String getLocalIp() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
logger.warn("获取本机IP失败!", e);
return EMPTY;
}
}Community answers on StackOverflow identified the root cause as a known bug in certain OpenJDK versions (7 and 8) where java.net.Inet4AddressImpl.getLocalHostName hangs while trying to resolve the system host name.
Typical symptoms include a 5‑second (or multiples thereof) pause before the host address is returned, which explains the observed startup delay.
Three remediation strategies were proposed:
Add explicit hostname mappings to /etc/hosts : 127.0.0.1 localhost .local ::1 localhost .local
Create an alias for the loopback address using ifconfig : sudo ifconfig en0 alias 127.0.0.1 alias 127.0.0.1
Upgrade the JDK to a version where the bug is fixed; the issue is known to affect certain releases of JDK 7 and 8.
The author applied the first solution (editing /etc/hosts ) and confirmed that the startup delay was completely eliminated.
Additional context includes a network diagram of the IPv6 setup and references to the author's other technical articles.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.