Results 1 to 4 of 4

Thread: processBuilder

  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Question processBuilder

    Since I am trying to become a better programmer I am trying out a few things that I think would be nice to know.

    One of these things would be to write a program that can call another program in the command line. Now I know that I can use processBuilder for this. And I also found a few nice examples.

    But does anyone also know if there are tutorials online where I can find how every part of this processBuilder works? Because in the examples I found, I can't realy tell how everything works exactly. And now is that the right part I would like to know...

    (I use Linux.)

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: processBuilder

    The Java API and documentation will always tell you everything about everything Java, its a great place to learn about a certain class(es).
    ProcessBuilder (Java 2 Platform SE 5.0)

  3. #3
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: processBuilder

    I just read the API doc, and I came up with the folowing code:

    Java Code:
     
    import java.io.IOException;
    import java.util.Map;
    
    public class SystemCommandExecutor {
    	
        public static void main(String[] args) {
            try {
                // String command we would like to send. 
                String command = "ls";
                // Create a new ProcessBuilder. 
                ProcessBuilder process = new ProcessBuilder(command); 
                // Map view of processBuilders environment. 
                Map<String, String> environment = process.environment(); 
                
                // The home directory. (naam nog toevoegen) 
                process.directory();
                // Start the processBuilder. 
                process.start(); 
                
                System.out.println();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} 
        }
    }
    I would like to call the terminal and show me what files are in the home directory.
    But how do I get the results?
    Last edited by Lund01; 10-11-2011 at 12:06 PM.

  4. #4
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: processBuilder

    I already found the answer. :)
    I needed to add:

    BufferedReader is = new BufferedReader(new InputStreamReader(shell.getInputStream()));

    Now it works perfect!

Similar Threads

  1. Invoke external program using ProcessBuilder
    By seongjoo in forum Advanced Java
    Replies: 6
    Last Post: 03-23-2011, 09:42 AM
  2. Using ProcessBuilder
    By efebatistaarda in forum Advanced Java
    Replies: 4
    Last Post: 02-11-2011, 08:40 PM
  3. Using ProcessBuilder
    By akhil in forum New To Java
    Replies: 0
    Last Post: 07-21-2009, 10:12 PM
  4. Run ant scripts using ProcessBuilder
    By prsindagi in forum Advanced Java
    Replies: 6
    Last Post: 02-11-2009, 10:26 AM
  5. bug (?) in ProcessBuilder.redirectErrorStream(true);
    By gadasie in forum Advanced Java
    Replies: 3
    Last Post: 03-13-2008, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •