Coveraver Coveraver - 5 months ago 32
Java Question

How get active directory domain name in java

Tell me please
How can i get active directory domain name from java
I tried this
System.out.println(System.getenv("USERDOMAIN"));
but I only get the name of the computer

======================

I did so

InetAddress inet = InetAddress.getLocalHost();
InetAddress[] ips = InetAddress.getAllByName(inet.getCanonicalHostName());
usernameId.setText(System.getProperty("user.name"));
if (ips != null) {
for (int i = 0; i < ips.length; i++) {
String[] str = ips[i].toString().split("/");
if (!(str[1].startsWith("169") || str[1].contains(":")))
System.out.println("Computer name: " + str[0] + "\nIp address: " + str[1]);
computernameId.setText(str[0]);


And i get ip address and computername.domainname

Answer

Try using

System.out.println(System.getenv("USERDNSDOMAIN"));

If that does not work, you can (as James Tanner said) try parsing through your system variables to find the one you want:

    Map<String, String> envMap = System.getenv();

    Iterator iter = envMap.entrySet().iterator();

    while (iter.hasNext()) {
        Map.Entry<String, String> pair = (Map.Entry<String, String>)iter.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
    }