• 0

File permissions in Unix


Question

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

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

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?

Link to comment
Share on other sites

  • 0

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.

Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

  • 0

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.

Link to comment
Share on other sites

This topic is now closed to further replies.