Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems with ProcessBuilder at linux

807605Sep 27 2007 — edited Sep 28 2007
Hello together,

I want use the linux command su (/bin/su) in a java program and receive the root shell if the password was right.
Therefore I tried the followoing approach:

import java.io.*;
import java.util.*;

public class sutest {

public static void main(String[] args) {
try{
ProcessBuilder builder = new ProcessBuilder();
builder.command( "bash", "-c", "su" );
builder.directory( new File("/") );
Process p = builder.start();

PrintStream printer = new PrintStream(p.getOutputStream());
String str="my password";
printer.println(str);
printer.flush();

Scanner s = new Scanner( p.getInputStream() ).useDelimiter( "\\Z" );

if (!s.hasNext()) {
Scanner sa = new Scanner(p.getErrorStream());
while (sa.hasNext()) {
System.out.println(sa.nextLine());
}
}

while (s.hasNext()) {
System.out.println( s.next() );
}
}catch(Exception ex){
ex.printStackTrace();
}

}

}

But the only thing I get is the following message:
su: must be run from a terminal
<img class="emoticon" src="images/emoticons/confused.gif" border="0" alt="" />
Are there any possibilities use su in my java application and how it is possible to set for su a terminal environment in java?

greetz itachy

p.s. sorry for my bad english<img class="emoticon" src="images/emoticons/happy.gif" border="0" alt="" />
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 26 2007
Added on Sep 27 2007
1 comment
1,169 views