eclipse-vertx/vert.x · error · IllegalArgumentException

KeyManagerFactory is not present or is not initialized yet

Error message

KeyManagerFactory is not present or is not initialized yet

What it means

The KeyManagerFactoryOptions constructor requires a KeyManagerFactory that is non-null and already initialized (its getKeyManagers() array is non-null and non-empty). Passing a freshly created but uninitialized factory or null throws IllegalArgumentException.

Source

Thrown at vertx-core/src/main/java/io/vertx/core/net/KeyManagerFactoryOptions.java:60

 * <pre>
 * // with a KeyManager
 * options.setKeyCertOptions(KeyCertOptions.wrap(keyManager));
 *
 * // with a KeyManagerFactory
 * options.setKeyCertOptions(KeyCertOptions.wrap(keyManagerFactory));
 * </pre>
 *
 * @author <a href="mailto:hakangoudberg@hotmail.com">Hakan Altindag</a>
 */
class KeyManagerFactoryOptions implements KeyCertOptions {

  private final KeyManagerFactory keyManagerFactory;

  KeyManagerFactoryOptions(KeyManagerFactory keyManagerFactory) {
    if (keyManagerFactory == null
      || keyManagerFactory.getKeyManagers() == null
      || keyManagerFactory.getKeyManagers().length == 0) {
      throw new IllegalArgumentException("KeyManagerFactory is not present or is not initialized yet");
    }
    this.keyManagerFactory = keyManagerFactory;
  }

  KeyManagerFactoryOptions(X509KeyManager keyManager) {
    this(new KeyManagerFactoryWrapper(keyManager));
  }

  private KeyManagerFactoryOptions(KeyManagerFactoryOptions other) {
    this.keyManagerFactory = other.keyManagerFactory;
  }

  @Override
  public KeyCertOptions copy() {
    return new KeyManagerFactoryOptions(this);
  }

  @Override

View on GitHub (pinned to fb308bd8c3)

Solutions

  1. Call kmf.init(keyStore, password) before wrapping it in KeyManagerFactoryOptions
  2. Ensure the KeyStore loads successfully and contains at least one key entry
  3. If wrapping a single X509KeyManager, use the X509KeyManager constructor overload instead

Example fix

// before
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
options.setKeyCertOptions(new KeyManagerFactoryOptions(kmf)); // throws
// after
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keyStore, keyStorePassword);
options.setKeyCertOptions(new KeyManagerFactoryOptions(kmf));
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(kmf, "KeyManagerFactory required");
kmf.init(keyStore, password); // must succeed before wrapping
if (kmf.getKeyManagers() == null || kmf.getKeyManagers().length == 0) {
  throw new IllegalStateException("KeyManagerFactory not initialized");
}
options.setKeyCertOptions(new KeyManagerFactoryOptions(kmf));

Type guard

boolean isInitializedKmf(KeyManagerFactory kmf) {
  return kmf != null && kmf.getKeyManagers() != null && kmf.getKeyManagers().length > 0;
}

Try / catch

try {
  kmf.init(keyStore, password.toCharArray());
  options.setKeyCertOptions(new KeyManagerFactoryOptions(kmf));
} catch (IllegalArgumentException | KeyStoreException | NoSuchAlgorithmException e) {
  throw new IllegalStateException("TLS key material setup failed", e);
}

Prevention

When it happens

Trigger: Calling new KeyManagerFactoryOptions(kmf) where kmf is null, or where kmf was created via KeyManagerFactory.getInstance(...) but init(KeyStore, char[]) was never called (so getKeyManagers() returns null or empty).

Common situations: Building TLS client/server options with a custom KeyManagerFactory and forgetting the init() step; a factory whose KeyStore failed to load so init silently left it unusable; refactors that moved init() out of the setup path.

Related errors


AI-assisted analysis of eclipse-vertx/vert.x@fb308bd8c3 (2026-09-06). Data as JSON: /api/errors/f229db3bee3632f0. Report an issue: GitHub.