• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Runtime.getRuntime().exec() with no output...  RSS feed

 
Sam Smoot
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a process that requires me to write to a file on an external system and does not generate output on the calling system. How do I execute / invoke the Runtime.getRuntime().exec(myCode); process to make this happen correctly? The command is running on a Unix based system, but the output is going to an IBM z/OS mainframe.

I can execute the "myCode" commands in the native Unix environment as a script and it sends the data to the z/OS side without a problem. I just need this to do this without external intervention (automated process).

Any / all help is greatly appreciated..
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can. There are two Streams associated with the Process you are creating, and you have to empty their output. I had to try this myself a few weeks ago and this thread includes an example of what I tried. If you go through the javadoc comment for the StreamEater class it gives a reference to Javaworld 6� years ago, with more details.
I found I was getting deadlock if I didn't empty the Streams, and a ConcurrentModificationException if I tried to read the ArrayLists before they were completely filled; hence the 1-second delay.

You can't not empty the Streams, but having emptied them you are under no obligation to use what you have recorded.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit confused what the original poster really wants (PostRealCode), but have had many problems with spawned processes' standard output and standard error.

Even when you think there will be no output, it is unwise to skip consuming the streams, as I have recently found out. In error conditions, low-level routines may produce output that you haven't asked for.

If you use Java 5's ProcessBuilder class, you can tell it to combine the two streams into one. This means you only need one thread to consume both the standard output and standard error; with Runtime.exec() you need two.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!