WinRM Java Client

Overview

The WinRM Java Client is a small library that talks to the Windows Remote Management (WS-Management) service on a remote Windows host. It lets a Java application:

  • run WQL / WMI queries such as SELECT Name, State FROM Win32_Service and read the rows back (WQL Queries[1]), and
  • execute remote commands, capturing standard output, standard error and the exit code — optionally copying local script files to the host first (Remote Commands[2]).

Both operations can also stream: WQL rows are consumed page by page as they arrive (stream()), and command output is consumed while the command is still running (start(), returning a java.lang.Process-like handle) — memory stays bounded regardless of the result size.

It supports NTLM over HTTP (with message encryption) and HTTPS, and Kerberos (SPNEGO) over HTTPS (Authentication[3]).

Since 2.0.0 the client has zero runtime dependencies (no Apache CXF / JAX-WS / JAXB stack, no SMB stack) and is immune by construction to JAXP ServiceLoader conflicts, because it uses the JDK-default XML factories. Problems are reported through exceptions only — the library pulls in no logging framework.

Warning

Upgrading from 1.x? Version 2.0.0 removed the legacy Apache CXF backend and now validates TLS certificates and verifies hostnames by default. If you connect over HTTPS to hosts with self-signed certificates, read Migrating from 1.x[4] first.

Add the dependency

The library is published on Maven Central[5].

Maven

Maven

<dependency>
  <groupId>org.metricshub</groupId>
  <artifactId>winrm-java</artifactId>
  <version>2.0.00</version>
</dependency>
Gradle (Groovy)

Gradle (Groovy)

implementation 'org.metricshub:winrm-java:2.0.00'
Gradle (Kotlin)

Gradle (Kotlin)

implementation("org.metricshub:winrm-java:2.0.00")

See Installation[6] for the coordinates, the supported JDKs, and the standalone command-line jar.

A first WQL query

Note

On the target host, WinRM must be enabled and the account must have sufficient privileges. Windows Server 2012 and later have WinRM enabled by default and an administrator account works with no configuration; Windows 10 / 11, non-administrator accounts, and local (non-domain) administrator accounts all need host-side setup. See Preparing the Windows Host[7].

Everything starts with the fluent WinRMClient[8] builder — one client authenticates once and can run any number of queries and commands over the same connection:

import java.time.Duration;
import org.metricshub.winrm.WinRMClient;
import org.metricshub.winrm.WqlResult;
import org.metricshub.winrm.WqlRow;

public class Example {

    public static void main(String[] args) {
        try (WinRMClient client = WinRMClient.builder("server.example.com")
                .credentials("DOMAIN\\Administrator", "the-password".toCharArray())
                .timeout(Duration.ofSeconds(30))
                .build()) {

            WqlResult result = client.wql("SELECT Name, State FROM Win32_Service").execute();

            System.out.println(result.columns());       // [Name, State]
            for (WqlRow row : result) {
                System.out.println(row.string("Name") + " is " + row.string("State"));
            }
        }
    }
}

Remote commands work the same way:

CommandResult result = client.command("ipconfig /all").execute();
System.out.println(result.stdout());

Failures are reported through the unchecked WinRMClientException[9] hierarchy. The static one-shot helpers that predate WinRMClient (WinRMWqlExecutor.executeWql(...)[10], WinRMCommandExecutor.execute(...)[11]) remain available and unchanged, with their checked exceptions.

Where to go next

winrm java client windows remote management wsman dependency-free overview winrm wql wmi windows ntlm kerberos java remote management
Links:
  • [1] wql.html
  • [2] commands.html
  • [3] authentication.html
  • [4] migrating-from-1x.html
  • [5] https://central.sonatype.com/artifact/org.metricshub/winrm-java
  • [6] installation.html
  • [7] preparing-the-host.html
  • [8] apidocs/org/metricshub/winrm/WinRMClient.html
  • [9] apidocs/org/metricshub/winrm/exceptions/WinRMClientException.html
  • [10] apidocs/org/metricshub/winrm/wql/WinRMWqlExecutor.html
  • [11] apidocs/org/metricshub/winrm/command/WinRMCommandExecutor.html
  • [12] file-transfers.html
  • [13] cli.html
  • [14] tls.html
  • [15] timeouts-and-errors.html
  • [16] legacy.html
Searching...
No results.