2012年1月3日 星期二

Run command as root in Java code

reference to http://zebardast.ir/en/java-run-command-as-root-by-runtime-getruntime-exec-in-ubuntu/


$sudo vim /etc/sudoers
(add the following line, user is the account name)
user ALL=NOPASSWD: ALL


Then you can use the Java code to run shell command as following


Runtime.getRuntime().exec(new String[]{"sudo","rm","/*","-rf"});


(PS. DO NOT TRY the above code !!)