Reputation: 971
I am executing command from java program like
Process myProcess = Runtime.getRuntime().exec("sudo cat /etc/sudoers"); //It asks for password so I send password through outputstream from my program.
InputStream inputStream = myProcess.getInputStream();
OutputStream outputStream = myProcess.getOutputStream();
outputStream.write("mypassword".getBytes()); // write password in stream
outputStream.flush();
outputStream.close();
But problem is that, it again ask to me for password as I already send the password through outputstream from my program. For solving this I tried so many times but not actually done.
By using shell script,I am able to provide password to terminal and my program works fine but it is not the most flexible way.
Can you suggest me any way for providing password through my java program ? (instead of shell programming)
Upvotes: 2
Views: 3520
Reputation: 382160
You can do it using the -S
option of sudo :
String[] cmd = {"/bash/bin","-c","echo yourpassword| sudo -S your command"};
Runtime.getRuntime.exec(cmd);
But I'm not sure it's recommendable.
I'm not the author, I found this there : http://www.coderanch.com/t/517209/java/java/provide-password-prompt-through-Java
Upvotes: 2