0

Possible Duplicate:
Execute a linux shell command with “sudo” using java, without entering the required password

I want to execute sudo command on linux through java program but it ask for password. But I want to run prog without password,please tell me. My prog is

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
Community
  • 1
  • 1
user 912177
  • 43
  • 1
  • 7
  • 6
    http://stackoverflow.com/questions/5168755/execute-a-linux-shell-command-with-sudo-using-java-without-entering-the-requi – 0xAX May 24 '12 at 06:52

2 Answers2

1

Grant your user the permissions by using sudo. Have a look here: http://www.ducea.com/2006/06/18/linux-tips-password-usage-in-sudo-passwd-nopasswd/

For example:

/etc/sudoers
admin    ALL = NOPASSWD: ALL
ErikFWinter
  • 173
  • 4
0

If you can get it to run that would mean a security flaw.

Said so...

  1. Grant your user permissions (become sudoer)
  2. Invoke your java program with sudo

    $ sudo java JavaCommandProg

This will prompt you for introducing sudo password when running jvm and then you can invoke sudo from inside your Java program.

OnaBai
  • 40,767
  • 6
  • 96
  • 125