Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the name of a file's owner in Java on OSX?

Tags:

java

macos

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

like image 629
James Skidmore Avatar asked Jun 22 '10 20:06

James Skidmore


People also ask

How do I find the owner of a file in OSX?

Question: Q: How can I know the owner of a file in OS X? How can I know what's the owner of a file in finder > get info? so, if there're permissions for several users, you'll see them listed first, THE BOTTOM-MOST USER IS THE OWNER.

How will you find the ownership of a file?

The normal method would be to right click on the file in Explorer, select Properties, click the Security tab and click Ownership. This will then show the current owner and give the option to take ownership.


2 Answers

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:

  • Java 7 NIO Tutorial - Managing Metadata
like image 73
BalusC Avatar answered Oct 18 '22 19:10

BalusC


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.

like image 4
Donal Fellows Avatar answered Oct 18 '22 19:10

Donal Fellows