我想通过java程序在linux上执行sudo命令,但它要求输入密码。但我想在没有密码的情况下运行 prog,请告诉我。我的编是
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