Results 1 to 4 of 4
  1. #1
    Godjikung is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default OutputStream.flush() problem

    I would like to send the same command multiple times to cmd. My source code is here:

    Java Code:
    Process process = Runtime.getRuntime ().exec ("cmd");
    OutputStream stdin = process.getOutputStream();
    for(int i=0;i<=250;i++)
    {
          line="command"+"\n";
          stdin.write(line.getBytes());
          stdin.flush();
          System.out.println(i);
    }
    stdin.close();
    My program doesn't complete the loop. It works about 200times and hang without any error message. I can only close program by end process.

    Help me please.
    Sorry for repeating topics.
    Last edited by Godjikung; 01-28-2011 at 07:10 PM. Reason: add stdin declaration

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    When you write to the output stream of a process, you are writing to the underlying pipe. That pipe has a fixed buffer size, and once the buffer fills, the write commands will block until the process you executed reads from the pipe, which it apparently doesn't.

    I'm not an expert on using "cmd", but I think if you want it to read from stdin you have to give it a switch, since the default is to just pop up a terminal window. You might try "cmd /K".

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

    Default

    Todally is right; when you send "command\n" to your process it sends back something line "unrecognized command" trough one of its output steams (stdout or stderr); you don't read from that stream so it fills up and blocks when its full.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Godjikung is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks for your quick reply.
    I read the stdout and stderr too. It didn't return "unrecognize command". It working properly. (My real command isn't "command". - -) My program still works properly when the loop is less than 200.

    I have tried "cmd /K" but it didn't change anything. Is there another method that can send command to cmd multiple times? or what should I do :confused:

Similar Threads

  1. OutputStream.flush() problem
    By Godjikung in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:48 PM
  2. GZIPOutputstream flush
    By Nardian in forum Networking
    Replies: 4
    Last Post: 09-25-2010, 06:09 PM
  3. How to flush gui from using old data
    By ShinTec in forum AWT / Swing
    Replies: 1
    Last Post: 06-10-2010, 03:18 PM
  4. Outputstream not streaming.
    By dantestein in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 08:36 AM
  5. flush method
    By bugger in forum New To Java
    Replies: 0
    Last Post: 11-11-2007, 08:39 PM

Posting Permissions

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