可能重複:
Execute a linux shell command with 「sudo」 using java, without entering the required password如何從java程序運行沒有密碼的sudo命令?
我想通過java程序在Linux上執行sudo命令,但它索要密碼。 但我想運行沒有密碼編,請告訴我。 我的編是
public class JavaCommandProg {
public static void main(String args[]) throws IOException, InterruptedException {
String command="sudo cat /etc/sudoers";
Process myProcess =null;
try {
Runtime runtime=Runtime.getRuntime();
File file=new File("/home/users/admin/temp");
myProcess = runtime.exec(command,null,file);
myProcess.waitFor();
InputStreamReader myIStreamReader = new InputStreamReader(myProcess.getInputStream());
BufferedReader br=new BufferedReader(myIStreamReader);
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
} catch (IOException anIOException) {
System.out.println(anIOException);
}
}
}
Compling like: javac JavaCommandProg.java
and : java JavaCommandProg
http://stackoverflow.com/questions/5168755/execute-a-linux-shell-command-with-sudo-using-java-without-entering-the-requi – 0xAX