Jump to content



Photo

File permissions in Unix

java unix file permissions

  • Please log in to reply
4 replies to this topic

#1 timsweb

timsweb

    Neowinian Senior

  • Joined: 19-February 04
  • Location: UK

Posted 17 August 2012 - 10:24

Hi all,

I was wondering how in Java I could detect if the software is running on unix and then if so, get the unix file permissions for a certain file in string form?

Thanks, Tim


#2 OP timsweb

timsweb

    Neowinian Senior

  • Joined: 19-February 04
  • Location: UK

Posted 21 August 2012 - 19:04

So after looking more into this, it seems I could do it by checking if in Unix with the path separator used, then if in Unix, I could execute a Unix command to get the file permissions string, but how do I execute a Unix command in Java and pull the result into a String object?

#3 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 22 August 2012 - 01:54

I'm not sure exactly how much research you did into this, but it doesn't sound like you have the right solution for platform detection or the right idea for permission checking. You can find excellent Java API documentation online. It is always best to use functions in the standard library rather than using Runtime.getRuntime().exec() to launch a native executable, which I suspect is what you are attempting for permissions checking.

Without actually seeing your code, I can say that your "solution" for patform detection sounds like an unreliable hack. Instead, you should probably be using System.getProperty("os.name") to check which OS your program was launched on. There is an excellent example of this technique available here.

Similarly, you should probably be using the file class built into the Java standard library for permissions checking. A descent example of something somewhat similar can be found here.

I hope I have put you on the right path. If you have further questions, more specifics, such as the code you are struggling with, would be appreciated.

#4 OP timsweb

timsweb

    Neowinian Senior

  • Joined: 19-February 04
  • Location: UK

Posted 22 August 2012 - 15:06

Thanks xorangekiller,

The problem for me with using the file class in Java is that it only gives you access to basic permissions with three methods, canRead canWrite and canExecute where as if i am in unix, I will need the full permissions string which as shown in your example, it shows how to set it but not retieve it, any ideas how I could retieve it into a String?

Thanks, Tim

#5 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 23 August 2012 - 23:31

I'm not exactly sure how you want the permissions formatted "into a String", but you should be able to retrieve them using the Files.getPosixFilePermissions() function. If you need them in some special format as a String, it shouldn't be too difficult to write a wrapper function to do so.