HMCL-dev/HMCL · error · IllegalArgumentException

Illegal port:

Error message

Illegal port: 

What it means

Compact-constructor guard on the Http proxy record: the port must fit in the 16-bit unsigned range (0-65535); any out-of-range value is rejected at construction. Generic validation of a user-supplied or config-supplied port.

Solutions

  1. Pass a valid port in 0..65535; use 0 if 'default port' is intended
  2. Clamp/validate the port before constructing the record
  3. Fix the settings file so proxy.port holds a legal number
  4. Distinguish 'no port configured' from a numeric -1 sentinel in config loading

Example fix

// before
new ProxyOption.Http(host, -1, null, null)
// after
int port = rawPort < 0 ? 0 : rawPort;
new ProxyOption.Http(host, port, null, null)
Defensive patterns

Strategy: validation

Validate before calling

if (port < 0 || port > 0xFFFF)
    throw new IllegalArgumentException("Port must be 0..65535, got " + port);

Type guard

static boolean isValidPort(int port) { return port >= 0 && port <= 0xFFFF; }

Try / catch

try {
    option = new ProxyOption.Http(host, port, null, null);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Illegal port")) {
        port = 0; // or re-prompt
    } else throw e;
}

Prevention

When it happens

Trigger: Constructing Http with a negative port, a port above 65535, or an unvalidated config value like -1 used as 'unset'.

Common situations: Settings JSON where port was never entered and defaults to -1, users typing port numbers beyond 65535, string-to-int parsing artifacts.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of HMCL-dev/HMCL@24702dc5a0 (2026-09-10). Data as JSON: /api/errors/3d201a44eef33196. Report an issue: GitHub.

Appendix: source

Thrown at HMCLCore/src/main/java/org/jackhuang/hmcl/game/ProxyOption.java:47

record Http(@NotNull String host, int port, @Nullable String username,
            @Nullable String password) implements ProxyOption {
    public Http {
        if (StringUtils.isBlank(host)) {
            throw new IllegalArgumentException("Host cannot be blank");
        }
        if (port < 0 || port > 0xFFFF) {
            throw new IllegalArgumentException("Illegal port: " + port);
        }
    }
}

View on GitHub (pinned to 24702dc5a0)