|
25 | 25 | import java.io.IOException;
|
26 | 26 | import java.io.InputStream;
|
27 | 27 | import java.io.InputStreamReader;
|
| 28 | +import java.net.InetAddress; |
28 | 29 | import java.net.ServerSocket;
|
29 | 30 | import java.net.Socket;
|
| 31 | +import java.net.UnknownHostException; |
30 | 32 | import java.nio.file.Path;
|
31 | 33 | import java.util.Locale;
|
| 34 | +import java.util.Optional; |
32 | 35 | import java.util.concurrent.TimeUnit;
|
33 | 36 | import java.util.concurrent.TimeoutException;
|
34 | 37 | import java.util.logging.Level;
|
@@ -98,10 +101,24 @@ public synchronized void start() throws IOException, TimeoutException, Interrupt
|
98 | 101 | }
|
99 | 102 | this.port = getAvailablePort();
|
100 | 103 |
|
| 104 | + // Try to align the localhost address across java & golang emulator |
| 105 | + // This should fix issues on systems that default to ipv4 but the jvm is started with |
| 106 | + // -Djava.net.preferIPv6Addresses=true |
| 107 | + Optional<String> localhostAddress = Optional.empty(); |
| 108 | + try { |
| 109 | + localhostAddress = Optional.of(InetAddress.getByName(null).getHostAddress()); |
| 110 | + } catch (UnknownHostException e) { |
| 111 | + } |
| 112 | + |
101 | 113 | // Workaround https://bugs.openjdk.java.net/browse/JDK-8068370
|
102 | 114 | for (int attemptsLeft = 3; process == null; attemptsLeft--) {
|
103 | 115 | try {
|
104 |
| - process = Runtime.getRuntime().exec(String.format("%s -port %d", executable, port)); |
| 116 | + String cmd = executable.toString(); |
| 117 | + if (localhostAddress.isPresent()) { |
| 118 | + cmd += String.format(" -host [%s]", localhostAddress.get()); |
| 119 | + } |
| 120 | + cmd += String.format(" -port %d", port); |
| 121 | + process = Runtime.getRuntime().exec(cmd); |
105 | 122 | } catch (IOException e) {
|
106 | 123 | if (attemptsLeft > 0) {
|
107 | 124 | Thread.sleep(1000);
|
|
0 commit comments