Java GroupPrincipal look up by group name

Description

Java GroupPrincipal look up by group name

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;

public class Main {

   public static void main(String[] args) {
      try {// w w w.j  a va 2 s  .  co m
       UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
       GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName("Administrators");
       UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("Tom");
       System.out.println(groupPrincipal.getName());
       System.out.println(userPrincipal.getName());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}



PreviousNext

Related