Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to capture text os output

Status
Not open for further replies.

brinker

Programmer
May 31, 2001
48
CA
Hello,

I have been working on a problem that involves executing a command line statement in java such as gcc -c fileName. I want to capture the screen output (ie. errors, success) which generally appears as text such as :

compile successful

or

Error in line 23

Since these commands do not work in a window, but appear as lines in the OS display, is there a way to capture them. The following syntax does not work:

----------------------------------------------------------------------------------
Process extProcess=rt.exec(cmd);
extProcess.waitFor();

// receive the output of external program
InputStream extInput = extProcess.getInputStream();
int bytesAvailable = extInput.available();
if (bytesAvailable > 0)
{
byte[] extInputData = new byte[bytesAvailable];
extInput.read(extInputData);
System.out.println(new String(extInputData));
for (int i=0;i<bytesAvailable;i++) {
s+=(char)extInputData;
}
JOptionPane.showMessageDialog(null,s ,&quot;testing it&quot;,JOptionPane.INFORMATION_MESSAGE);
outputS=s;
System.out.println(&quot;\nFile Compiled Successfully&quot;);
// System.exit(0);
}

------------------------------------------------------------------------

probably because jave sees no bytes available. I have tried creating a command shell using '/c', but this does not help.
 
have you tried redirecting the output from the process into a flat file and then read that into your program???

gcc -c filename > output.txt
 
the command :

gcc -c filename>output.txt creates an output file, but it does not capture the screen output (ie. error in line 23, etc).

Brinker
 
Has it occurred to you that the process output that you are trying to capture is probably on stderr and not stdout?

Instead of using getInputStream, use getErrorStream.
 
Thanks everyone for your help.

The problem was with the standard error stream and not with stdout. It is solved. Yippie!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top