2012-05-24 96 views
-3

我想使用eclipse IDE从Java PROG运行sudo命令,但它不运行,并在控制台不索要密码,不显示任何输出消息,请告诉我 我PROG是如何使用eclipse IDE从java程序运行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); 
     } 
    } 
} 
+2

重复的http://stackoverflow.com/questions/10732526/how-to-run-sudo-command-without-password-from-java-program你不应该发表两次相同的问题,特别是在同一小时内。如果你被允许这个网站将被淹没。给人们一些时间来回应,如果你没有得到任何足够好的回答,请尝试编辑你的问题以使其更清晰 –

+0

重复http://stackoverflow.com/questions/5168755/execute-a-linux-shell -command与 - 使用的Java-而不进入须藤-最requi –

回答

1

Process.waitFor是一个阻塞呼叫。

在你做myProcess.waitFor之前,你需要创建线程来处理程序I/O。