How to read all inputstream data?
hi' this is really weird and i'm really confused about this.
i want to read all the error message from inputstream using BufferedReader (readline()). But what i got is just the endline of the error message (not the whole error message).
this is my code : (named FileAction)
publicvoid compileFile(File fileName)throws IOException
{
try
{
Process proc = Runtime.getRuntime().exec("Javac "+fileName.getAbsolutePath());
errorCatch (proc.getErrorStream());
}
catch (Exception ec)
{
System.err.println("The exception is: "+ ec.getMessage());
}
}
publicvoid errorCatch (InputStream istr)throws IOException
{
BufferedReader inputBfr =new BufferedReader (new InputStreamReader(istr));
while (inputBfr.readLine() !=null)
{
System.out.println(inputBfr.readLine());
}
}
My code try to read some given file, compile it and show the result (error message).
This is file that i try to compile it (named softmain) :
public cass softmain// ERROR. it should be public class softmain
{
publicstaticvoid main (String[] args)
{
}
}
When i try to run FileAction the output is just :
public cass softmain
1 error
but, when i'm compile softmain using the manual way (type javac softmain.java at prompt), the result are :
C:\>javac softmain.java
softmain.java:1:'class' or'interface' expected
public cass softmain
^
1 error
My question is :
how to get the error message like when i'm using the manual compile way? why my program only can read the endline of error message(not the whole error message)?
thanks a lot. i hope my question easy to understand...
The following line softmain.java:1: 'class' or 'interface' expectedis not a errror message it's for hint, so use proc.getInputStream() instead of proc..getErrorStream() to get all.
i still got the same problem, the output only the endline.
why i cannot read like the manual compile way?
public void compileFile(File fileName) throws IOException
{
try
{
Process proc = Runtime.getRuntime().exec("Javac "+fileName.getAbsolutePath());
errorCatch (proc.getInputStream(),proc.getErrorStream());
}
catch (Exception ec)
{
System.err.println("The exception is: "+ ec.getMessage());
}
}
public void errorCatch (InputStream inStream, InputStream istr) throws IOException
{
BufferedReader readinStream = new BufferedReader (new InputStreamReader(inStream));
BufferedReader readIstr = new BufferedReader (new InputStreamReader(istr));
StringBuffer sBuff = new StringBuffer();
while (readinStream.readLine() != null)
{
sBuff.append(readinStream.readLine()+"\n");
}
while (readIstr.readLine() != null)
{
sBuff.append(readIstr.readLine()+"\n");
}
System.out.println(sBuff.toString());
}
is there any way to read them all?
thanks...
> while (readinStream.readLine() != null)
> {
> sBuff.append(readinStream.readLine()+"\n");
> }
>
> while (readIstr.readLine() != null)
> {
>sBuff.append(readIstr.readLine()+"\n");
> }
Both these loops are reading a line, testing it for null, then reading another line and printing it. So you will only print every second line, and you will print 'null' at end of stream. Your loops should look more like this:
[code]
String line;
while ((line = in.readLine()) != null)
; // do something with 'line'
[code]
ejpa at 2007-7-15 5:32:20 >

hey, it really works...thanks a lot man. you're da best..
hi' can i ask one more? i need to implement this.
can i call commandprompt with java? cos i tried
Runtime.getRuntime().exec("cmd");
it can't? there's no error but the commandprompt not show too.what's wrong?
how to execute another java class file with java code?
thanks...
You should read, digest and implemplement the approach given in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html .
hi' i already read it. i think i'm still confused about it. cos when i'm try "GoodWindowsExec" from the website. I got the null mesage, not like from the example. i don't know what's wrong.
i'm still confused about this too. The example only give about how to read output and show it. but it doesn't explain about interact to the commandprompt. What i mean like this :
When i create some program that need to receive input from user, i think it's more efficient if i just call the commandprompt and give argument to execute the class file like "cmd /C java FileName". but this is doesn't work.
How to call window commandprompt with specific argument?
can anyone help me...
thanks a lot...
>>> When i create some program that need to receive input from user, >>>i think it's more efficient if i just call the commandprompt and give >>>argument to execute the class file like "cmd /C java FileName". but >>>this is doesn't work.
>>>How to call window commandprompt with specific argument?
i think java can't call windows commandprompt dialog directly. cos windows commandprompt is independent process. but i'm still confuse about this too. Why we can call notepad, calc, etc directly. but not for cmd. i hope i got this answer too...
Do you know that your reading a line and skiping onther. your code
while (readinStream.readLine() != null) //here your reading a line but you don't do anything with it.
> {
> sBuff.append(readinStream.readLine()+"\n");
// here your reading next line.
> }
the right code is
String str'
while ( (Srr=readinStream.readLine()) != null) > {
> sBuff.append(Str+"\n");
> }
You can run a promt command, but as you know promt command will not give you any thing tell you ask something. so you need to get the InputStream and outputstream of your Promt, write your command through outputstream and get the result through inputstream
Hi' i already use InputStream and OutputStream too, try to look at this : http://forum.java.sun.com/thread.jspa?threadID=788253&tstart=0, i don't know what is the problem? can you help...Thanks a lot...