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!

Viewing OS output from trying to execute a command line statement

Status
Not open for further replies.

brinker

Programmer
May 31, 2001
48
CA
Hello all,

I am trying to construct a program that tries to execute a command line statement inside a Java program. I have determined that the syntax below works well if the statement is valid (ie. runs a self contained c program). However, if the statement is invalid, there is nothing returned. For example, let's say the program bee is there. The statement will work and print out the output from running bee. However, if bee does not exist, I can not view the errors from within Java. The code I am using is :

try {
Process extProcess = rt.exec(compileField.getText());
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.exit(0);
}
}
catch (Exception e)
{
}

Can anybody help me. I want the user of my program to be able to see if a certain file exists or not. This means that the OS will output error messages if the program does not exist (which should be able to be captured by Java into a string)...there should be nothing output if the program does exist.


thanks

Brinker
 
you can use the java.io.File class to check if the file exists first. e.g.

File myFile = new File( &quot;bee&quot; );

if( myFile.exists() )
{
// Do your thing here
}
else
{
// Error message or exception
}
 
you can also use the getErrorStream method onthe Process class to read any errors from the command line
 
Thanks LittleWing,

I am still having difficulty though in trying to use the getErrorStream command. I have noticed that when I try to use the line rt.exec(dir) for example, I get an error message even though this is a valid command line statement in dos.

The sytax I was using is something like this:

try {
Process extProcess = rt.exec(compileField.getText());
extProcess.waitFor();

// receive the output of external program
InputStream extInput = extProcess.getInputStream();
InputStream errStream = extProcess.getErrorStream();
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);
}
}
catch (Exception e)
{
System.out.println(&quot;\n ----- Error in Compilation -----\n&quot;);
System.out.println(&quot;\n &quot;+e.toString());
}

However, I can not print out the error message (command line) since this all occurs within a try {} section. I need to print out the command line errors only if an exception occurs. Do you or anyone else have any ideas?

thanks

Brinker
 
I hope that there is a way to do this within java, as this is the crux of most compilers, IDEs, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top