-1

Hi i need to change the user/group of certain Files through JAVA. I have seen roughly the same Posix example evrywhere.

  public interface POSIX extends Library {
    public int chmod(String filename, int mode);
    public int chown(String filename, int userId, int groupId);
    }

The problems with this is that you have to know the PID and GID, but to do that you will have to pass throught Runtime to get them and so i don't really see the use of chown method. I tried this

  public interface POSIX extends Library {
    public int chmod(String filename, int mode);
    public int chown(String filename, int userId, int groupId);
    public int chown(String filename, String user, String group);
    }

but the after passing the string parameters for user and group, ls -l shows me 806789808 806789776 dor user/group of the file. So is there a way to pass the user and group as Strings ?

user2548720
  • 109
  • 2
  • 2
  • 7

1 Answers1

0

The simplest way to pass them as strings, is to

Runtime.exec(new String[] { "chown", user, filename });

What chown does is look up the name for the id before making the system call.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • and btw i have java 6 and exec doesn't get 3 strings as parameters. – user2548720 Nov 01 '13 at 20:51
  • @user2548720 The OS only deals with numbers. The record on disk only have numbers. The translation of numbers into names is a job for the tools. Java 7 doesn't have varargs either, fixing... – Peter Lawrey Nov 01 '13 at 20:57