diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java index ff6c918d803ee..32c5ee31a66dd 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.ObjectOutputStream; import java.net.InetAddress; +import java.net.ServerSocket; import java.net.Socket; import java.net.URI; import java.net.UnknownHostException; @@ -40,7 +41,7 @@ public abstract class QuarkusDevModeLauncher { final Pattern validDebug = Pattern.compile("^(true|false|client|[0-9]+)$"); - final Pattern validPort = Pattern.compile("^[0-9]+$"); + final Pattern validPort = Pattern.compile("^-?[0-9]+$"); public class Builder> { @@ -369,7 +370,7 @@ protected void prepare() throws Exception { } } if (port <= 0) { - throw new Exception("The specified debug port must be greater than 0"); + port = getRandomPort(); } if (debug != null && debug.equalsIgnoreCase("client")) { @@ -479,6 +480,12 @@ protected void prepare() throws Exception { } } + private int getRandomPort() throws IOException { + try (ServerSocket socket = new ServerSocket(0)) { + return socket.getLocalPort(); + } + } + private InetAddress getInetAddress(String host) throws UnknownHostException { if ("localhost".equals(host)) { return InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });