Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage Linux file permissions in Java?

Is there any mechanism to get and set the file/directory permissions?

For example, I want to show the permisssions of a file in a shell way:

-rwxr-xr--

Is it possible to do this using Java?

  • I know that there are some methods in the File class to know if the file canExecute, canRead and canWrite, but AFAIK this info is for the current user only. I need to know the whole octal number, for example 755, so I need to get it from the user, from group and from others.
  • I know that Java7 brings Posix operations, but how could do this using a smaller JRE?
  • I would like not to use a command like ls, or chmod.
like image 428
Daniel Peñalba Avatar asked Jun 01 '26 08:06

Daniel Peñalba


1 Answers

If you can use external libraries, there are several:

  • JPosix
  • Posix for java
  • jnr-posix

If an entire library seems a hassle, creating a JNI wrapper that calls the lstat C function and returns the access mode takes you about 10 minutes. Here's a tutorial that creates such a wrapper for the isatty and ttyname functions.

like image 138
Joni Avatar answered Jun 03 '26 20:06

Joni