6


Is there any way to read/set Unix rights for a file and check if process has user/group/other rights in Java? I know it's not a problem with JNI, but I need cross-platform solution without JNI.

3
  • 3
    You need a crossplatform solution to read unix file rights? Oh well, that seems problematic for some java unrelated reasons. If the information that java offers with the File API aren't enough you'll need to use some unix specific solution. The NIO File class in Jdk7 though supports both ACLs and Unix rights - but I don't see how that'd be portable.. you'll have to stick with the highlevel information Commented Jun 22, 2011 at 20:18
  • 1
    "Unix file rights" (permissions?) doesn't sound very "cross-platform"! Commented Jun 22, 2011 at 20:18
  • I think it's pretty clear that the author meant "across platforms that support Unix file modes" so that he didn't have to write JNI and compile a library for each platform he wanted to support. Commented Jun 23, 2011 at 18:34

3 Answers 3

9
Set<PosixFilePermission> filePerm = null;
try {
    filePerm = Files.getPosixFilePermissions(Paths.get("/app/data/abc.txt"));
} catch (IOException e) {
    e.printStackTrace();
}
String permission = PosixFilePermissions.toString(filePerm);

Supported from Java 1.7, not cross-platform.

Sign up to request clarification or add additional context in comments.

Comments

5

This is going to be possible with Java 7, which is going to be released this summer, using the classes in the package java.nio.file.attribute. See the JDK 7 API documentation.

Java 6 and older do not have anything in the standard library to work with Unix file system rights.

So, by using the Java 7 API you won't need JNI but as Voo says it is ofcourse not going to be cross-platform, because Windows and other non-Unix systems do not support Unix file rights.

3 Comments

Java7 may support unix filerights, but the solution won't be any more portable than using JNI if you really need such low level information (after all you generally can't map Unix filerights to ACLs)
If you can map Unix rights on Windows rights it should be possible to list Windows/Unix ACLs to a list of Unix rights.
@Voo realize that with JNI you would need to rebuild the JNI parts for each unix type, e.g. Mac, Linux, Solaris and so on, so certainly this approach helps a great deal. The wording "cross platform" however is unfortunate as it brings the mind to e.g. Windows.
1

Currently where is no general file access rights management API in java. It is targeted to future java releases. As for now we have no more then File.canRead()/canWrite()/canExecute(), and File.setExecutable...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.