我想使用 eclipse IDE 从 java prog 运行 sudo 命令,但它没有运行,不要求输入密码,也不在控制台中显示任何输出消息,请告诉我 我的程序是
public class JavaCommand {
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);
}
}
}
最佳答案
Process.waitFor是一个阻塞调用。
在执行 myProcess.waitFor
之前,您需要创建线程来处理程序 I/O。
关于java - 如何使用 eclipse IDE 从 java 程序运行 sudo 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10734026/