I need to attribute a password to my system account (UTILISATEUR-PC/UTILISATEUR° using java code. Is there any java api or indication how to start to make this work?
Asked
Active
Viewed 155 times
0
-
I don't know if there is some java api to do it, but you'll need root permissions to do that – BackSlash Jan 28 '14 at 09:14
-
Is it possible to execute the command "net user username *" that allow to give a password to the ueser from java??? – junior developper Jan 28 '14 at 10:06
-
Yes, via `Runtime.exec`, as described by _Stephen C._ in his answer. But note: Java purpose is to "WORA" (write once, run everywhere), so everything you code in java should be system independent. Also note that your code won't run properly if your application doesn't have permissions to change the password for some user – BackSlash Jan 28 '14 at 10:48
-
Thanks for the explanation But i need to execute commands via Runtime.exec as an administrator. What should i add? – junior developper Jan 28 '14 at 12:17
-
Nothing, it's not something you can control. If the user opens the application with administrator rights, then your program will work, else it will fail – BackSlash Jan 28 '14 at 15:25
-
@BackSlash I did it after adding the file SUDO.cmd to c:/windows/system32 and add SUDO before the cammand. – junior developper Jan 28 '14 at 15:31
1 Answers
1
There is no standard Java API to do this.
You could identify an (operating system specific!!) external command to do this, and then invoke that command using Runtime.exec(...)
or equivalent.
However, I think you are better off doing this directly; e.g. by running commands from the command line by hand.
Tinkering around with system account passwords in a program:
requires elevated privilege, and programs that can elevate their privilege are a bad idea,
is potentially dangerous, since if the program messed it could effectively disable the account. (Exactly how dangerous that is depends on the importance of the account, and whether there are easy ways to undo the damage.)

Stephen C
- 698,415
- 94
- 811
- 1,216