This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Showing output from Runtime.getRuntime().exec  RSS feed

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program that executes an ant build.xml file. The exec is running ok and it is building, however I want to show the output of the build in a
textpane. I anm getting the InputStream from the Process class and displaying that in a System.out statement first. However all I'm getting is
the build.xml command. What do I need to do to get the output of the build? Here is the code:

Process process1 = Runtime.getRuntime().exec( "cmd /c cd c:/jaxmeproject" );

inStream = process1.getInputStream();
byte[] buffer = new byte[100];
for( int loop = 0; loop < buffer.length; loop++ )
{
buffer[loop] = 0;
}
inStream.read( buffer );
result = new String( buffer );
result = result.trim();
System.out.println( "results are = " + result );
 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me. This is the exec statement. The rest is the same.

Process process2 = Runtime.getRuntime().exec( "cmd /c c:/ant/bin/ant" );
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to our IO/Stream forum...
[ January 24, 2006: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason process1 has changed to process2. I'm guessing that's just a mistake. But if you've got two processes at once, you might want to post more code to show what you're doing exactly. Otherwise I'll assume you meant process1 both times.

You don't need that loop to initialize the array values to 0. Java guarantees that a newly-created array will have all values initialized to 0 if you don't explicitly initalize otherwise. (Or null, or false, if it's an array of objects or booleans, and there are some other special rules for "multidimensional" arrays, but never mind that for now...) As it happens you will probably end up coding this in such a way that it doesn't matter if there were nonzero values in the array anyway.

The bigger issue, however, is that read() is not guaranteed to read everything you need at once. It can return an incomplete read because the process is still busy generating output (slower than you can read it), or because there is more output than can fit into the array. So, you need to put the read in a loop, and keep looping until you're done. This is achieved by checking the return value of the read(), which tells how many bytes were read, and returns -1 if there is nothing more to be read. Also, it's probably easier to use an InputStreamReader to facilitate converting bytes to chars:

Or if you are using JDK 5, you could also use a Scanner:

[ January 24, 2006: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!