Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to interactive with the process generated by Runtime.getRuntime().exec() on Linux

 
Zhishan li
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My scenario is:
The shell command "sudo xxx" should be run by Runtime.getRuntime().exec() on linux.
It should wait for entering the password, but it not. And the command fails to executed.

So could you tell me how to upload my password to the process by Java??


I have the idea to enter the passwd by changing the shell command, like this "echo passwd | sudo xxx".
Or create a no passwd account. But I need the password entered by Java function.

Do me a favor. Thanks
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Zhishan and welcome to the Ranch! First and most important, are you familiar with situations When Runtime.exec() won't? If not, be sure to read all four parts of that article and apply the advice you find there.

You will need the -S flag to tell sudo to read its password from stdin.

Apply that, and if you still have problems then post your code in the form of a SSCCE (Short, Self Contained, Correct (Compilable), Example).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use gksudo rather than sudo then a password prompt will be displayed. I would also re-enforce Darryl's advice and say it is very very important that you read ALL the sections of the 'traps' article and implement ALL the recommendations. Failure to do so will just cause you grief.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic