2012-09-23 49 views
0

我做以下操作來獲取文件的所有者:錯誤嘗試使用UserPrincipal

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

這裏是我的代碼:

private String getOwner(File f) 
{ 
    Path p = Paths.get(f.getAbsolutePath()); 
    UserPrincipal owner = Files.getOwner(p); 
    return owner.getName(); 
} 

我得到一個「無法找到符號」錯誤。那就是:

...$ javac Delete.java 
Delete.java:38: error: cannot find symbol 
    UserPrincipal owner = Files.getOwner(p); 
    ^
    symbol: class UserPrincipal 
    location: class Delete 
1 error 

我知道錯誤的意思,我已經嘗試了幾種import語句:

java.security。*; (http://docs.oracle.com/javase/7/docs/api/

java.nio.file.attribute; (http://docs.oracle.com/javase/7/docs/api/

我甚至不得不問這個問題,但我不知道我會做錯什麼!

+2

你確定你有'java 7'嗎? –

+0

我很確定這意味着我有Java 7 ... javac -version/javac 1.7.0_06 – jacky

+0

向我們展示您得到的確切完整的錯誤消息。 –

回答

1

我能夠編譯和我的Mac上用下面的進口運行代碼:

import java.nio.file.*; 
import java.io.File; 
import java.io.IOException; 
import java.nio.file.attribute.UserPrincipal; 

,並採用以下格式:

private String getOwner(File f) throws IOException { 
    Path p = Paths.get(f.getAbsolutePath()); 
    UserPrincipal owner = Files.getOwner(p); 
    return owner.getName(); 
} 

您應該檢查,如果你真的使用Java 7在你編譯你的代碼的地方。

+0

啊,好的。我會嘗試這些。我在終端的同一個窗口中運行了javac -version,我用它來編譯,所以我認爲這是相同的地方! – jacky