-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);
        }
    }
}
2
  • 2
    Duplicate of stackoverflow.com/questions/10732526/… 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 Commented May 24, 2012 at 8:46
  • Duplicate stackoverflow.com/questions/5168755/… Commented May 24, 2012 at 8:51

1 Answer 1

1

Process.waitFor is a blocking call.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.