-3

I want to run sudo command from java prog using eclipse IDE,but it is not running and does not ask for password and not show any output message in console,please tell me My prog is

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);
        }
    }
}
user 912177
  • 43
  • 1
  • 7
  • 2
    Duplicate of http://stackoverflow.com/questions/10732526/how-to-run-sudo-command-without-password-from-java-program You should not post the same question twice, especially within the same hour. If you were allowed to to that this site would be flooded. Give people some time to respond, if you don't get any responses that's good enought, try to edit your question to make it more clear – John Snow May 24 '12 at 08:46
  • Duplicate http://stackoverflow.com/questions/5168755/execute-a-linux-shell-command-with-sudo-using-java-without-entering-the-requi – Bhavik Ambani May 24 '12 at 08:51

1 Answers1

1

Process.waitFor is a blocking call.

Before you do myProcess.waitFor, you need to have created threads to handle program i/o.

ewan.chalmers
  • 16,145
  • 43
  • 60