Sign in to follow this  
Followers 0

File permissions in Unix


5 posts in this topic

Posted

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

Share this post


Link to post
Share on other sites

Posted

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?

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.