6

I just need to know the owner's name of a file on Mac using Java -- how can I go about getting it? Thanks!

ruakh
  • 175,680
  • 26
  • 273
  • 307
James Skidmore
  • 49,340
  • 32
  • 108
  • 136

2 Answers2

16

That't not possible until Java 7. With Java 7, you could use Files#getOwner() for this.

Path          path     = Paths.get("/path/to/file.ext");
UserPrincipal owner    = Files.getOwner(path);
String        username = owner.getName();

Prior to Java 7, your best bet is running a command or JNI in combination with some platform native code.

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
4

This one is nasty. There is no standard method for getting the owner of a file (at least up to and including Java 6, which is the current production version for OSX). The easiest way right now is to use a ProcessBuilder to exec a call to stat with the name of the file as its argument, and then parse the result that will be returned on the standard output channel from the subprocess. Care must be taken when parsing the result because usernames on OSX might have spaces in.

Alternatively, use JNI.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215