Skip to content

Commit 09cf3cb

Browse files
committed
默认添加preferIPv4参数
1 parent c1396c0 commit 09cf3cb

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,33 @@ private Command generateCommandLine(Path nativeFolder) throws IOException {
190190
}
191191
}
192192

193+
try {
194+
boolean hasIPv4 = false;
195+
java.util.Enumeration<java.net.NetworkInterface> nets = java.net.NetworkInterface.getNetworkInterfaces();
196+
while (nets != null && nets.hasMoreElements() && !hasIPv4) {
197+
java.net.NetworkInterface nif = nets.nextElement();
198+
try {
199+
if (!nif.isUp() || nif.isLoopback()) continue;
200+
} catch (Throwable ignore) {
201+
// ignore and continue checking other interfaces
202+
continue;
203+
}
204+
java.util.Enumeration<java.net.InetAddress> addrs = nif.getInetAddresses();
205+
while (addrs.hasMoreElements()) {
206+
java.net.InetAddress addr = addrs.nextElement();
207+
if (addr instanceof java.net.Inet4Address && !addr.isLoopbackAddress()) {
208+
hasIPv4 = true;
209+
break;
210+
}
211+
}
212+
}
213+
if (hasIPv4) {
214+
res.addDefault("-Djava.net.preferIPv4Stack=", "true");
215+
}
216+
} catch (java.net.SocketException e) {
217+
LOG.warning("Failed to detect IPv4 address", e);
218+
}
219+
193220
final int javaVersion = options.getJava().getParsedVersion();
194221
final boolean is64bit = options.getJava().getBits() == Bits.BIT_64;
195222

0 commit comments

Comments
 (0)