run "sudo -i" command in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsn
    New Member
    • Sep 2007
    • 237

    run "sudo -i" command in java

    hello everyone. i am trying to run the sudo -i command in my mac.
    here is the code
    Code:
    	   String s = null;
    
    
            try {
                
    	   
                
                p = Runtime.getRuntime().exec(str);
                
                stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    
                // read the output from the command
                
                //System.out.println("Here is the standard output of the command:\n");
                /*while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }
                
                // read any errors from the attempted command
    
                //System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
                */
                //System.exit(0);
            }
            catch (IOException e) {
                System.out.println("exception happened - here's what I know: ");
                e.printStackTrace();
                System.exit(-1);
            }
        }
    in the beginning of the code p is the object of the Process class.

    this code is in a function and i cal this function twice, the first time is to run the sudo -i command and the second one for entering the password,
    the problem is when i enter the sudo -i command no output comes out. but for any other command the output comes out just fine.

    if any one has any ideas i would be grateful

    best regards
    hsn
  • hsn
    New Member
    • Sep 2007
    • 237

    #2
    first of all, please remove the commented areas the /* and */
    also, what i think the issue is that when you use this code with the sudo -i command the program pauses at line number 16. i checked it out it is not because of the while loop, if you remove the while loop the same issue will stay.

    regards
    hsn

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Your program blocks itself when the sudo sub-command wants input and doesn't produce any output. Use several threads for that purpose. This little article explains it all.

      kind regards,

      Jos

      Comment

      • hsn
        New Member
        • Sep 2007
        • 237

        #4
        hello Jos. i have done the foloowing code

        Code:
        import java.io.BufferedReader;
        import java.io.BufferedWriter;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        import java.io.OutputStream;
        import java.io.OutputStreamWriter;
        
        
        public class tests implements Runnable {
        	
        	private static Process p,p2;
        	private static Runtime rt;
        	private static String str;
        	
        	public static void main (String [] args){
        		try{
        		
        	        
        	       
        			tests t=new tests();
        			str="sudo -i";
        			t.run();
        			str="qzvvp";
        			t.run();
        			 
        			
        		}
        		catch(Exception e){
        			e.printStackTrace();
        			
        		}
        		
        	}
        
        	public void run() {
        		String s="";
        		try{ 
        			rt=Runtime.getRuntime();
        			
        			p = rt.exec(str);
        			 
        	        System.out.println(p.waitFor());
        	        BufferedReader  stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        			while( (s=stdInput.readLine())!=null){
        			System.out.println("> "+s);
        			}
        		}
        		catch(Exception e){
        			e.printStackTrace();
        			
        		}
        	}
        }
        I CREATED a thread which will run the process object with the requested command. but still the system freezes after entering the sudo -i command.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          That code doesn't make any sense: you use a single thread and make it wait for the completion of the run process. Read that article and see how they do it.

          kind regards,

          Jos

          Comment

          Working...