Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ProcessBuilder and I/O

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ProcessBuilder and I/O

    I'm trying to get my head around the ProcessBuilder class, so I built this test class. Needless to say, it doesn't work, but I can't figure out why. Here's the code:

    import java.io.*;
     
     
    /*Test Class*/
    public class TestMe
    {
    	public static void main(String[] args)
    	{
    		ProcessBuilder pb = null;
    		Process shell = null;
    		BufferedReader fromProcess = null;
    		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    		PrintWriter toProcess = null;
    		String command = "";
    		String line = "";
     
    		try
    		{
    			System.out.printf("-------------\n");
    			System.out.printf("Testing Process Builder%n");
    			pb = new ProcessBuilder ("/bin/bash");
    			System.out.printf("Merge stdout with stderr? %b%n", pb.redirectErrorStream());
    			if (pb.redirectErrorStream() != true)
    			{
    				System.out.printf("\tMerging both streams%n");
    				pb.redirectErrorStream(true);
    				System.out.printf("\tIt is now %b%n", pb.redirectErrorStream());
    			}
     
    			System.out.printf("Creating Shell process\n");
    			shell = pb.start();
    			System.out.printf("Getting stdout\n");
    			fromProcess = new BufferedReader (new InputStreamReader (shell.getInputStream()));
    			System.out.printf("Getting stdin\n");
    			toProcess = new PrintWriter (shell.getOutputStream(), true);
     
    			System.out.printf("Time to pass a command!%n");
    			System.out.printf("\tstdin :- ");
    			command = input.readLine();
    			System.out.printf("\tCommand is: %s%n", command);
    			System.out.printf("Handing command over to process...%n");
    			toProcess.print(command);
     
    			System.out.printf("Getting output...%n");
    			do
    			{
    				line = fromProcess.readLine();
    				System.out.printf("\t%s%n",line);
    			} while (line != null);
     
    			//Close I/O
    			toProcess.close();
    			fromProcess.flush();
    			fromProcess.close();
     
    			System.printf("It Worked!%n");
     
    		}
    		catch (Exception e)
    		{
    			System.out.printf("%nError: %s%n", e.getMessage());
    			e.printStackTrace();
    		}
     
    	} //End main method
     
    } //End class TestMe




    In my head, I should be able to pass a command (say, "ls") and get the output of the command printed out to the screen. Where am I going wrong? Any help would be greatly appreciated.


    Zo...


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ProcessBuilder and I/O

    See this article: When Runtime.exec() won't - JavaWorld