-1

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
1

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

thanks,now my program running
I'm glad you find it useful! BUT, watch out what you do inside your java program since you are sudoer (for good and for bad).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.