Checking if a computer is part of a domain
Different approaches to determine the domain membership of a computer

Usage of command line tools

If you use command line tools you have the burden to parse the output of the process.

A naive implementation to execute command line tools in windows:

public class CmdExecutor {


    private String cmdString;


    public CmdExecutor(String cmdString) {
        this.cmdString = cmdString;
    }


    public void execute() throws IOException, InterruptedException {

        final Process process = Runtime.getRuntime().exec("cmd /C " + cmdString);

        final BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line = br.readLine();

        while (line != null) {

            System.out.println(line);

            line = br.readLine();

        }

        process.waitFor();

    }


}


wmic

    public static void main(String[] args) throws IOException, InterruptedException {
        new CmdExecutor("wmic computersystem get domain").execute();
    }


systeminfo

    public static void main(String[] args) throws IOException, InterruptedException {
        new CmdExecutor("systeminfo | findstr /B /C:\"Domain\"").execute();
    }


LDAP access

If you have access to the LDAP interface of your Active Directory you can query LDAP for a specific computer account:

(&amp;(objectClass=computer)(name=MyPC))


Comparing the approches

Using JNA and the command line approaches on the local machine can only return the local state. The only reliable information you can get from LDAP. This especially relates to disabled or deleted computer accounts.

Querying LDAP and evaluating the result (including server not reachable) may be difficult. But as the ldap schema does not change frequently you can rely on the query parameters and the result fields to remain stable.

Command line tools and Windows system libraries tend to change in usage from time to time. That is a lot more probable than in LDAP. So you have to expect to adapt the result parsing or the parameter structure.

This you can say for the command line tools as well, but there is a higher abstraction. You can deal with standard command line tools very well and easily provide alternate implementations. This is even possible with JNA but you a dealing with system libraries and not with command line tools. (My personal view on this is that command line tools are easier to handle).