Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53667

How to create a folder in a directory where user need admin permission

Hi I need to put some files in some directory which myself as a user have no rights but for administrator has rights. In this case I want my program should ask for admin username and password. If i put correct admin username and password then i must be able to put the folder in that directory. Otherwise i should get a message box for wrong username and password

Thanks Sunil Kumar Sahoo

Upvotes: 0

Views: 3435

Answers (5)

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53667

Its not possible in java. without the permission of root/admin the user cannot craete any folder or directory in root file system. The folder needs admin access.

Upvotes: 1

BalusC
BalusC

Reputation: 1109292

Since Java 1.6 the java.io.File has the following methods:

  1. setReadable()
  2. setWritable()
  3. setExecutable()

Do a guess what they do. There's a overloaded method with a boolean ownerOnly. This should be the solution if you're already logged in as admin (even more; in Windows it's impossible to give a file admin-only rights if you're already not the admin).

Upvotes: 0

Alexsander Akers
Alexsander Akers

Reputation: 16024

Create a folder such that the permissions are as follows: (but you get a choice!)

Choose this option if you (and the 'others') want to be able to save, copy, or move files into this folder, but not see the contents of it:

 ___________________________________
|        | User   | R | W | X | Val |
|--------+--------+---+---+---+-----|
| user   | you    | X | √ | X |  2  |
| group  | admins | √ | √ | √ |  7  |
| others | N/A    | X | √ | X |  2  |
|________|________|___|___|___|_____|

Choose this option if you (and the 'others') should not have any privileges to this folder:

 ___________________________________
|        | User   | R | W | X | Val |
|--------+--------+---+---+---+-----|
| user   | you    | X | X | X |  0  |
| group  | admins | √ | √ | √ |  7  |
| others | N/A    | X | X | X |  0  |
|________|________|___|___|___|_____|

Does this solve your problem or am I misinterpreting it?

Upvotes: 0

Erich Kitzmueller
Erich Kitzmueller

Reputation: 36987

At least in Unix-like operating systems (e.g. Linux), you cannot possibly do that in Java, not even with JNI. You need a binary executable file (not a shell script, java program etc.), owned by root, with the set-user-ID-on-execution bit set. This program should (but technically, doesn't have to) check that you are permitted to do what you want to do (by asking you for the password or whatever) and do the operation for you. BTW, su and sudo are like this.

The other possibility, and I think that's the standard way in Windows, is to have a daemon program running (started by the admin so it has the necessary privileges) and this daemon waits for commands to process (by some means of IPC, network etc.), checks if the user should be allowed to do that (however this is determined) and eventually processes those commands.

Upvotes: 1

Suraj Chandran
Suraj Chandran

Reputation: 24801

AFAIK you cant do this with Java alone, you may need JNI.

Upvotes: 1

Related Questions