Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Question Alternative to ProcessBuilder

    I want to run the command below in Java.

    D:\> ipconfig > test.txt

    ProcessBuilder seems to be a fine answer, but it is not.
    ProcessBuilder does not support pipe.

    I tried the code below.

    List<String> commands = new ArrayList<String>();
    commands.add("ipconfig");
    commands.add(">");
    commands.add("test.txt");
    ProcessBuilder processBuilder = new ProcessBuilder(commands);
    processBuilder.directory(new File("D:\\Test"));
    Process process = process = processBuilder.start();
    process.waitFor();

    I expect the result where test.txt is created under the D:\\Test.
    But the file is not created.

    I want to know an alternative method.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,251
    Rep Power
    19

    Default Re: Alternative to ProcessBuilder

    The tips in this article apply equally to processes launched using ProcessBuilder: When Runtime.exec() won't. Make sure you read all the parts.

    Also, go through these links:
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default Re: Alternative to ProcessBuilder

    No native process supports a pipe nor IO redirection; it's a shell (cmd.exe or bash or csh) that does that; you have to start a shell with your process as an argument and you have to feed all that to an exec( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. processBuilder
    By Lund01 in forum New To Java
    Replies: 3
    Last Post: 10-11-2011, 01:07 PM
  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

Tags for this Thread

Posting Permissions

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